Challenge - 5 Problems
Request Object Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of accessing req.method in an Express.js route?
Consider this Express.js route handler:
What will be the output when a client sends a GET request to '/test'?
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); });Attempts:
2 left
💡 Hint
The req.method property holds the HTTP method used for the request.
✗ Incorrect
The req.method property in Express.js contains the HTTP method of the incoming request, such as GET, POST, PUT, etc. Since the route is accessed with a GET request, req.method will be "GET".
📝 Syntax
intermediate2: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 */ });
Attempts:
2 left
💡 Hint
Query parameters are part of the URL after the '?' symbol.
✗ Incorrect
Query parameters are accessed via req.query in Express.js. req.params is for route parameters, req.body is for POST data, and req.headers is for HTTP headers.
🔧 Debug
advanced2:00remaining
What error occurs when accessing req.body without body-parser middleware?
Given this Express.js code:
What happens if the client sends JSON data but the app does not use any body parsing middleware?
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); });Attempts:
2 left
💡 Hint
Without middleware, req.body is not automatically parsed.
✗ Incorrect
Without body-parser or equivalent middleware, req.body is undefined. Trying to access req.body.name causes a TypeError because you cannot read properties of undefined.
🧠 Conceptual
advanced2: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?
Attempts:
2 left
💡 Hint
One property preserves the original request URL including the query string.
✗ Incorrect
req.originalUrl contains the full URL path and query string as requested by the client. req.url may be modified by middleware. req.path excludes query string. req.baseUrl is the matched route base.
❓ state_output
expert2: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']); });
Attempts:
2 left
💡 Hint
HTTP headers are case-insensitive but stored in lowercase in Node.js.
✗ Incorrect
Node.js converts all header names to lowercase. The value of 'content-type' header sent as 'application/json' remains exactly that string.