0
0
Expressframework~20 mins

req.params for route parameters in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Params Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does req.params contain in this route?
Consider this Express route:
app.get('/user/:id', (req, res) => { res.send(req.params); });

What will be the output if a client requests /user/42?
Express
app.get('/user/:id', (req, res) => { res.send(req.params); });
A{"user":"42"}
B{"params":"42"}
C{}
D{"id":"42"}
Attempts:
2 left
💡 Hint
Look at how the route defines the parameter name after the colon.
📝 Syntax
intermediate
2:00remaining
Which route correctly captures two parameters in req.params?
You want to capture both category and itemId from the URL. Which route definition is correct?
Aapp.get('/shop/:category/:itemId', (req, res) => { res.send(req.params); });
Bapp.get('/shop/:category,itemId', (req, res) => { res.send(req.params); });
Capp.get('/shop/:category&:itemId', (req, res) => { res.send(req.params); });
Dapp.get('/shop/:category?/:itemId?', (req, res) => { res.send(req.params); });
Attempts:
2 left
💡 Hint
Parameters are separated by slashes, not commas or ampersands.
🔧 Debug
advanced
2:00remaining
Why does req.params not contain expected values?
Given this code:
app.get('/post/:postId', (req, res) => { res.send(req.params.post); });

What happens when a client requests /post/123?
Express
app.get('/post/:postId', (req, res) => { res.send(req.params.post); });
AThe response is undefined because req.params.post does not exist.
BThe response is '123' as expected.
CThe server throws a SyntaxError.
DThe response is an empty object {}.
Attempts:
2 left
💡 Hint
Check the exact parameter name used in the route and in the code.
state_output
advanced
2:00remaining
What is the value of req.params for this route and URL?
Route:
app.get('/files/:fileName.:ext', (req, res) => { res.send(req.params); });

URL requested: /files/report.pdf
What does req.params contain?
Express
app.get('/files/:fileName.:ext', (req, res) => { res.send(req.params); });
A{"fileName":"report","ext":".pdf"}
B{"fileName":"report.pdf"}
C{"fileName":"report","ext":"pdf"}
D{}
Attempts:
2 left
💡 Hint
Express supports dot-separated parameters in routes.
🧠 Conceptual
expert
2:00remaining
Which statement about req.params is true?
Choose the correct statement about req.params in Express routes.
A<code>req.params</code> contains key-value pairs for all URL query parameters.
B<code>req.params</code> only contains parameters defined in the route path with colons.
C<code>req.params</code> is always an empty object unless middleware is used.
D<code>req.params</code> merges route parameters and body parameters automatically.
Attempts:
2 left
💡 Hint
Think about how Express separates route parameters from query and body data.