Bird
Raised Fist0
Expressframework~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does req.method represent in an Express request?
easy
A. The HTTP method used by the client (e.g., GET, POST)
B. The full URL including domain name
C. The IP address of the client
D. The server's response status code

Solution

  1. Step 1: Understand the purpose of req.method

    req.method holds the HTTP method used by the client to make the request, such as GET, POST, PUT, DELETE.
  2. Step 2: Differentiate from other request properties

    req.url gives the path and query, IP address is separate, and response status is not part of the request object.
  3. Final Answer:

    The HTTP method used by the client (e.g., GET, POST) -> Option A
  4. Quick Check:

    req.method = HTTP method [OK]
Hint: Remember: method means action type like GET or POST [OK]
Common Mistakes:
  • Confusing req.method with req.url
  • Thinking req.method gives client IP
  • Assuming req.method is server response code
2. Which of the following is the correct way to check if a request is a POST method in Express?
easy
A. if (req.url === 'POST')
B. if (req.url === '/post')
C. if (req.method = 'POST')
D. if (req.method === 'POST')

Solution

  1. Step 1: Identify the property holding HTTP method

    The HTTP method is stored in req.method, so we compare it to the string 'POST'.
  2. Step 2: Check syntax correctness

    if (req.method === 'POST') uses strict equality (===) to compare req.method to 'POST', which is correct. if (req.method = 'POST') uses assignment (=) which is wrong.
  3. Final Answer:

    if (req.method === 'POST') -> Option D
  4. Quick Check:

    Check req.method with === 'POST' [OK]
Hint: Use === to compare req.method with 'POST' string [OK]
Common Mistakes:
  • Using assignment (=) instead of comparison (===)
  • Checking req.url instead of req.method
  • Comparing req.method to lowercase 'post'
3. Given this Express code snippet, what will be logged if a client sends a GET request to /home?user=123?
app.use((req, res, next) => {
  console.log(req.method);
  console.log(req.url);
  next();
});
medium
A. GET /home?user=123
B. POST /home?user=123
C. GET /home
D. POST /home

Solution

  1. Step 1: Identify the HTTP method from the request

    The client sends a GET request, so req.method will be 'GET'.
  2. Step 2: Identify the URL including query string

    req.url includes the path and query string, so it will be '/home?user=123'.
  3. Final Answer:

    GET /home?user=123 -> Option A
  4. Quick Check:

    req.method = GET, req.url includes query [OK]
Hint: req.url includes path plus query string [OK]
Common Mistakes:
  • Ignoring query string in req.url
  • Assuming req.method is POST by default
  • Confusing req.url with just the path
4. What is wrong with this Express middleware code if it aims to log only POST requests?
app.use((req, res, next) => {
  if (req.method = 'POST') {
    console.log('POST request received');
  }
  next();
});
medium
A. Missing parentheses around req.method
B. Using assignment (=) instead of comparison (===) in the if condition
C. Logging message is incorrect
D. next() is called before the if condition

Solution

  1. Step 1: Check the if condition syntax

    The condition uses = which assigns 'POST' to req.method instead of comparing it. This causes a bug.
  2. Step 2: Identify correct comparison operator

    Use === to compare values in JavaScript, so the condition should be req.method === 'POST'.
  3. Final Answer:

    Using assignment (=) instead of comparison (===) in the if condition -> Option B
  4. Quick Check:

    Use === for comparison, not = [OK]
Hint: Use === to compare, not = which assigns [OK]
Common Mistakes:
  • Using = instead of === in if condition
  • Thinking next() must be before if
  • Assuming parentheses needed around req.method
5. You want to create an Express middleware that logs a message only when the client requests the URL /api/data with a GET method. Which code snippet correctly implements this?
hard
A. app.use((req, res, next) => { if (req.method === 'POST' && req.url === '/api/data') { console.log('GET request to /api/data'); } next(); });
B. app.use((req, res, next) => { if (req.method = 'GET' || req.url === '/api/data') { console.log('GET request to /api/data'); } next(); });
C. app.use((req, res, next) => { if (req.method === 'GET' && req.url === '/api/data') { console.log('GET request to /api/data'); } next(); });
D. app.use((req, res, next) => { if (req.method === 'GET' && req.url === '/api') { console.log('GET request to /api/data'); } next(); });

Solution

  1. Step 1: Check method and URL conditions

    The middleware should check if req.method is exactly 'GET' and req.url is exactly '/api/data'.
  2. Step 2: Verify logical operators and syntax

    app.use((req, res, next) => { if (req.method === 'GET' && req.url === '/api/data') { console.log('GET request to /api/data'); } next(); }); uses strict equality (===) and logical AND (&&) correctly. app.use((req, res, next) => { if (req.method = 'GET' || req.url === '/api/data') { console.log('GET request to /api/data'); } next(); }); uses assignment (=) and OR (||) which is wrong. Options C and D check wrong method or URL.
  3. Final Answer:

    app.use((req, res, next) => { if (req.method === 'GET' && req.url === '/api/data') { console.log('GET request to /api/data'); } next(); }); -> Option C
  4. Quick Check:

    Check req.method === 'GET' AND req.url === '/api/data' [OK]
Hint: Use === and && to check method and exact URL [OK]
Common Mistakes:
  • Using = instead of ===
  • Using || instead of &&
  • Checking wrong URL or method
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard