Bird
Raised Fist0
Node.jsframework~20 mins

Request object properties in Node.js - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Request Object Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of accessing req.method in an Express.js route?
Consider this Express.js route handler:
app.get('/test', (req, res) => { res.send(req.method); });

What will be the output when a client sends a GET request to '/test'?
Node.js
app.get('/test', (req, res) => { res.send(req.method); });
A"POST"
B"GET"
C"/test"
Dundefined
Attempts:
2 left
💡 Hint
The req.method property holds the HTTP method used for the request.
📝 Syntax
intermediate
2:00remaining
Which option correctly accesses the query parameter 'id' from the request object in Express.js?
You want to get the value of the query parameter 'id' from the URL '/user?id=123'. Which code correctly accesses it inside a route handler?
Node.js
app.get('/user', (req, res) => { /* access id here */ });
Aconst id = req.params.id;
Bconst id = req.body.id;
Cconst id = req.query.id;
Dconst id = req.headers.id;
Attempts:
2 left
💡 Hint
Query parameters are part of the URL after the '?' symbol.
🔧 Debug
advanced
2:00remaining
What error occurs when accessing req.body without body-parser middleware?
Given this Express.js code:
app.post('/submit', (req, res) => { res.send(req.body.name); });

What happens if the client sends JSON data but the app does not use any body parsing middleware?
Node.js
app.post('/submit', (req, res) => { res.send(req.body.name); });
AReferenceError: req is not defined
BSyntaxError: Unexpected token in JSON
Cres.send sends the correct name value
DTypeError: Cannot read property 'name' of undefined
Attempts:
2 left
💡 Hint
Without middleware, req.body is not automatically parsed.
🧠 Conceptual
advanced
2:00remaining
Which property of the request object contains the full URL path including query string?
In Express.js, which property of the request object gives the full URL path with query parameters included?
Areq.originalUrl
Breq.path
Creq.url
Dreq.baseUrl
Attempts:
2 left
💡 Hint
One property preserves the original request URL including the query string.
state_output
expert
2:00remaining
What is the value of req.headers['content-type'] after a JSON POST request?
A client sends a POST request with JSON data and header 'Content-Type: application/json'. Inside the Express.js route, what is the value of req.headers['content-type']?
Node.js
app.post('/data', (req, res) => { res.send(req.headers['content-type']); });
A"application/json"
B"Application/JSON"
Cundefined
D"application-json"
Attempts:
2 left
💡 Hint
HTTP headers are case-insensitive but stored in lowercase in Node.js.

Practice

(1/5)
1. Which property of the Node.js request object contains the HTTP method used by the client, such as GET or POST?
easy
A. req.method
B. req.url
C. req.headers
D. req.body

Solution

  1. Step 1: Understand the request object properties

    The request object has properties like method, url, headers, and body that describe the client's request.
  2. Step 2: Identify the property for HTTP method

    The HTTP method (GET, POST, etc.) is stored in req.method.
  3. Final Answer:

    req.method -> Option A
  4. Quick Check:

    HTTP method = req.method [OK]
Hint: HTTP method is always in req.method [OK]
Common Mistakes:
  • Confusing req.url with HTTP method
  • Using req.body to get method
  • Trying to find method in req.headers
2. Which of the following is the correct way to access the URL path from the request object in Node.js?
easy
A. req.route
B. req.path
C. req.url
D. req.address

Solution

  1. Step 1: Recall the property for URL path

    The request object stores the full URL path in req.url.
  2. Step 2: Verify other options

    req.path is not standard in Node.js core, req.route is used in some frameworks but not for URL path, and req.address is invalid.
  3. Final Answer:

    req.url -> Option C
  4. Quick Check:

    URL path = req.url [OK]
Hint: URL path is stored in req.url [OK]
Common Mistakes:
  • Using req.path which is not standard in Node.js
  • Confusing req.route with URL
  • Trying to access req.address which doesn't exist
3. Consider the following Node.js code snippet:
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?
medium
A. application/json
B. content-type
C. undefined
D. Error

Solution

  1. Step 1: Understand how headers are accessed

    Headers in Node.js request object are stored in lowercase keys inside req.headers. So content-type is the correct key.
  2. Step 2: Check the code output

    The code returns req.headers['content-type'], which matches the header sent by the client: application/json.
  3. Final Answer:

    application/json -> Option A
  4. Quick Check:

    Header content-type value = application/json [OK]
Hint: Headers keys are lowercase in req.headers [OK]
Common Mistakes:
  • Using 'Content-Type' instead of 'content-type' key
  • Expecting req.headers to be case-sensitive
  • Assuming undefined if header not found
4. What is wrong with this Node.js code snippet that tries to read JSON data from the request body?
const http = require('http');
const server = http.createServer((req, res) => {
  const data = req.body;
  res.end(data);
});
server.listen(3000);
medium
A. res.end cannot send data
B. req.body is undefined without parsing the data
C. req.body is a function, not a property
D. Missing require for http module

Solution

  1. Step 1: Understand how request body works in Node.js

    In Node.js core, req.body is not automatically populated. You must collect data chunks and parse them manually or use middleware.
  2. Step 2: Identify the error in the code

    The code tries to access req.body directly, which will be undefined, causing the response to send undefined.
  3. Final Answer:

    req.body is undefined without parsing the data -> Option B
  4. Quick Check:

    req.body needs manual parsing [OK]
Hint: req.body is empty unless parsed manually or with middleware [OK]
Common Mistakes:
  • Assuming req.body is auto-filled in Node.js core
  • Trying to send undefined data without error
  • Confusing res.end usage
5. You want to log the client's IP address and the requested URL path in a Node.js HTTP server. Which code snippet correctly accesses these properties from the request object?
hard
A. console.log(req.connection.remoteAddress, req.path);
B. console.log(req.ip, req.path);
C. console.log(req.address, req.route);
D. console.log(req.socket.remoteAddress, req.url);

Solution

  1. Step 1: Identify how to get client IP in Node.js

    The client IP is accessible via req.socket.remoteAddress or req.connection.remoteAddress. The req.socket is the modern standard.
  2. Step 2: Identify how to get URL path

    The URL path is stored in req.url. Other options like req.path or req.route are not standard in Node.js core.
  3. Step 3: Compare options

    console.log(req.socket.remoteAddress, req.url); uses req.socket.remoteAddress and req.url, which is correct and modern.
  4. Final Answer:

    console.log(req.socket.remoteAddress, req.url); -> Option D
  5. Quick Check:

    IP = req.socket.remoteAddress, URL = req.url [OK]
Hint: Use req.socket.remoteAddress and req.url for IP and path [OK]
Common Mistakes:
  • 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