0
0
Expressframework~10 mins

req.method and req.url in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - req.method and req.url
Incoming HTTP Request
Express Server Receives Request
Access req.method
Check HTTP method type (GET, POST, etc.)
Access req.url
Get the requested URL path
Use method and url to decide response
Send Response to Client
When Express gets a request, it reads req.method to know the HTTP method and req.url to know the requested path, then uses these to decide how to respond.
Execution Sample
Express
app.use((req, res) => {
  console.log(req.method);
  console.log(req.url);
  res.end('Done');
});
This code logs the HTTP method and URL of each request, then sends a simple response.
Execution Table
StepActionreq.methodreq.urlOutput
1Request arrives: GET /homeGET/homeLogs 'GET' and '/home'
2Request arrives: POST /submitPOST/submitLogs 'POST' and '/submit'
3Request arrives: DELETE /item/5DELETE/item/5Logs 'DELETE' and '/item/5'
4Request arrives: PUT /updatePUT/updateLogs 'PUT' and '/update'
5No more requests--Server waits for next request
💡 Execution continues as server handles each incoming request, logging method and URL each time.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
req.method-GETPOSTDELETEPUT-
req.url-/home/submit/item/5/update-
Key Moments - 2 Insights
Why does req.method change with each request?
Because req.method reflects the HTTP method of the current request, it updates each time a new request arrives, as shown in execution_table rows 1 to 4.
Is req.url the full website address?
No, req.url only contains the path and query part of the URL, not the domain or protocol, as seen in the logged values in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is req.method at step 3?
ADELETE
BPOST
CGET
DPUT
💡 Hint
Check the 'req.method' column at step 3 in the execution_table.
At which step does req.url equal '/submit'?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'req.url' column in execution_table to find '/submit'.
If a new request with method PATCH and url '/edit' arrives, what will req.method and req.url be?
Areq.method: GET, req.url: /edit
Breq.method: PATCH, req.url: /home
Creq.method: PATCH, req.url: /edit
Dreq.method: POST, req.url: /submit
💡 Hint
req.method and req.url always reflect the current request's method and URL.
Concept Snapshot
req.method and req.url in Express

- req.method: HTTP method of the request (GET, POST, etc.)
- req.url: URL path requested by client
- Use these to decide how to respond
- Changes with each incoming request
- Access inside middleware or route handlers
Full Transcript
In Express, when a client sends a request, the server receives it and creates a request object called req. This object has a property called method, which tells us the HTTP method used like GET or POST. It also has a property called url, which shows the path the client wants, like '/home' or '/submit'. By checking req.method and req.url, the server can decide what to do next. For example, it can log these values and send a response. Each new request updates these properties to match the new request's details.