0
0
Node.jsframework~10 mins

Request object properties in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Request object properties
Incoming HTTP Request
Node.js Server Receives Request
Request Object Created
Access Properties: method, url, headers, body
Use Properties to Handle Request
When a client sends a request, Node.js creates a request object. We then read its properties like method, url, headers, and body to decide how to respond.
Execution Sample
Node.js
const http = require('http');
const server = http.createServer((req, res) => {
  console.log(req.method);
  console.log(req.url);
  res.end('Done');
});
server.listen(3000);
This code logs the HTTP method and URL of each incoming request.
Execution Table
StepActionreq.methodreq.urlOutput
1Request received: GET /homeGET/homeLogs 'GET' and '/home'
2Request received: POST /submitPOST/submitLogs 'POST' and '/submit'
3Request received: PUT /api/dataPUT/api/dataLogs 'PUT' and '/api/data'
4No more requests--Server keeps running, waiting for requests
💡 Server runs continuously; stops only if manually closed.
Variable Tracker
VariableStartAfter 1After 2After 3
req.methodundefinedGETPOSTPUT
req.urlundefined/home/submit/api/data
Key Moments - 2 Insights
Why does req.method change with each request?
Because each incoming request can use a different HTTP method, as shown in execution_table rows 1-3.
Is req.url always the same?
No, req.url changes depending on the path requested by the client, as seen in execution_table rows 1-3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is req.method at step 2?
AGET
BPOST
CPUT
DDELETE
💡 Hint
Check the 'req.method' column at step 2 in the execution_table.
At which step does req.url equal '/api/data'?
AStep 3
BStep 2
CStep 1
DNo step
💡 Hint
Look at the 'req.url' column in the execution_table.
If a new request with method 'DELETE' and url '/remove' arrives, what will req.method be after that request?
AGET
BPOST
CDELETE
DPUT
💡 Hint
Refer to variable_tracker showing req.method changes per request.
Concept Snapshot
Request object properties in Node.js:
- req.method: HTTP method (GET, POST, etc.)
- req.url: Requested path
- req.headers: Request headers
- req.body: Data sent by client
Use these to understand and respond to client requests.
Full Transcript
When a client sends an HTTP request to a Node.js server, Node.js creates a request object. This object holds important details about the request. The main properties are req.method, which tells us the HTTP method like GET or POST, and req.url, which shows the path the client wants. By reading these properties, the server can decide how to respond. For example, if req.method is GET and req.url is '/home', the server might send the homepage. These properties change with each new request. The server keeps running, waiting for new requests, updating req.method and req.url each time.