0
0
Expressframework~20 mins

Route parameters in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Route Parameters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when accessing /user/42?
Consider this Express route:
app.get('/user/:id', (req, res) => { res.send(`User ID is ${req.params.id}`); });

What will the server respond with when a client requests /user/42?
Express
app.get('/user/:id', (req, res) => { res.send(`User ID is ${req.params.id}`); });
AUser ID is 42
BError: Cannot read property 'id' of undefined
CUser ID is undefined
DUser ID is :id
Attempts:
2 left
💡 Hint
Look at how route parameters are accessed via req.params.
📝 Syntax
intermediate
2:00remaining
Which route definition correctly captures a product ID?
You want to define a route in Express that captures a product ID from the URL like /product/123. Which of these route definitions is correct?
Aapp.get('/product/:productId?', (req, res) => { ... });
Bapp.get('/product/productId', (req, res) => { ... });
Capp.get('/product/:productId', (req, res) => { ... });
Dapp.get('/product/:productId*', (req, res) => { ... });
Attempts:
2 left
💡 Hint
Route parameters start with a colon and match one segment.
🔧 Debug
advanced
2:00remaining
Why does this route not capture the ID parameter?
Given this Express route:
app.get('/order/:id', (req, res) => { res.send(`Order ID: ${req.params.orderId}`); });

What will happen when a client requests /order/99?
Express
app.get('/order/:id', (req, res) => { res.send(`Order ID: ${req.params.orderId}`); });
AOrder ID: undefined
BOrder ID: 99
CError: Cannot read property 'orderId' of undefined
D404 Not Found
Attempts:
2 left
💡 Hint
Check the name used in the route parameter and the name used in req.params.
state_output
advanced
2:00remaining
What is the value of req.params after requesting /blog/2023/07?
Consider this Express route:
app.get('/blog/:year/:month', (req, res) => { res.json(req.params); });

What JSON will the server send when a client requests /blog/2023/07?
Express
app.get('/blog/:year/:month', (req, res) => { res.json(req.params); });
A{}
B{"year":"2023","month":"07"}
C{"month":"07"}
D{"year":"2023/07"}
Attempts:
2 left
💡 Hint
Each colon-prefixed segment captures one part of the URL path.
🧠 Conceptual
expert
3:00remaining
Which option correctly explains route parameter behavior with optional segments?
Given this route:
app.get('/archive/:year/:month?', (req, res) => { res.send(req.params); });

Which statement is true about accessing /archive/2022 and /archive/2022/12?
AAccessing /archive/2022 sets req.params to { year: '2022', month: null } and /archive/2022/12 sets req.params to { year: '2022', month: '12' }
BAccessing /archive/2022 causes a 404 error because :month is missing
CAccessing /archive/2022 sets req.params to { year: '2022', month: undefined } and /archive/2022/12 sets req.params to { year: '2022', month: '12' }
DAccessing /archive/2022 sets req.params to { year: '2022' } and /archive/2022/12 sets req.params to { year: '2022', month: '12' }
Attempts:
2 left
💡 Hint
The question mark after :month makes it optional.