The request object holds all the information about what a user sends to your server. It helps you understand and respond to their needs.
Request object properties in Node.js
Start learning this pattern below
Jump into concepts and practice - no test required
req.propertyName
req.url
req.method
req.headers['content-type']req.body
This simple Express server listens for POST requests to '/submit'. It reads the request method, URL, content-type header, and JSON body sent by the user. Then it sends back these details as a response.
import express from 'express'; const app = express(); app.use(express.json()); app.post('/submit', (req, res) => { const method = req.method; const url = req.url; const contentType = req.headers['content-type']; const data = req.body; res.send(`Method: ${method}\nURL: ${url}\nContent-Type: ${contentType}\nData: ${JSON.stringify(data)}`); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
Always use middleware like express.json() to parse JSON bodies before accessing req.body.
Headers are case-insensitive but usually accessed in lowercase in Node.js.
Request properties help you understand what the user wants so you can respond correctly.
The request object holds all details about what the user sends to your server.
You access properties like req.method, req.url, req.headers, and req.body to get this information.
Using these properties helps you build dynamic and responsive web servers.
Practice
Solution
Step 1: Understand the request object properties
The request object has properties likemethod,url,headers, andbodythat describe the client's request.Step 2: Identify the property for HTTP method
The HTTP method (GET, POST, etc.) is stored inreq.method.Final Answer:
req.method -> Option AQuick Check:
HTTP method = req.method [OK]
- Confusing req.url with HTTP method
- Using req.body to get method
- Trying to find method in req.headers
Solution
Step 1: Recall the property for URL path
The request object stores the full URL path inreq.url.Step 2: Verify other options
req.pathis not standard in Node.js core,req.routeis used in some frameworks but not for URL path, andreq.addressis invalid.Final Answer:
req.url -> Option CQuick Check:
URL path = req.url [OK]
- Using req.path which is not standard in Node.js
- Confusing req.route with URL
- Trying to access req.address which doesn't exist
const http = require('http');
const server = http.createServer((req, res) => {
res.end(req.headers['content-type']);
});
server.listen(3000);
If a client sends a request with header Content-Type: application/json, what will be the output?Solution
Step 1: Understand how headers are accessed
Headers in Node.js request object are stored in lowercase keys insidereq.headers. Socontent-typeis the correct key.Step 2: Check the code output
The code returnsreq.headers['content-type'], which matches the header sent by the client:application/json.Final Answer:
application/json -> Option AQuick Check:
Header content-type value = application/json [OK]
- Using 'Content-Type' instead of 'content-type' key
- Expecting req.headers to be case-sensitive
- Assuming undefined if header not found
const http = require('http');
const server = http.createServer((req, res) => {
const data = req.body;
res.end(data);
});
server.listen(3000);Solution
Step 1: Understand how request body works in Node.js
In Node.js core,req.bodyis not automatically populated. You must collect data chunks and parse them manually or use middleware.Step 2: Identify the error in the code
The code tries to accessreq.bodydirectly, which will beundefined, causing the response to sendundefined.Final Answer:
req.body is undefined without parsing the data -> Option BQuick Check:
req.body needs manual parsing [OK]
- Assuming req.body is auto-filled in Node.js core
- Trying to send undefined data without error
- Confusing res.end usage
Solution
Step 1: Identify how to get client IP in Node.js
The client IP is accessible viareq.socket.remoteAddressorreq.connection.remoteAddress. Thereq.socketis the modern standard.Step 2: Identify how to get URL path
The URL path is stored inreq.url. Other options likereq.pathorreq.routeare not standard in Node.js core.Step 3: Compare options
console.log(req.socket.remoteAddress, req.url); usesreq.socket.remoteAddressandreq.url, which is correct and modern.Final Answer:
console.log(req.socket.remoteAddress, req.url); -> Option DQuick Check:
IP = req.socket.remoteAddress, URL = req.url [OK]
- Using req.ip which is Express-specific
- Using req.path which is not in Node.js core
- Trying to access req.address or req.route which don't exist
