0
0
Node.jsframework~20 mins

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

Choose your learning style9 modes available
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.