Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a route that captures a user ID parameter.
Express
app.get('/user/[1]', (req, res) => { res.send(`User ID is ${req.params.id}`); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using *id or ?id instead of :id
Forgetting the colon before the parameter name
✗ Incorrect
The colon : before id defines a route parameter in Express.
2fill in blank
mediumComplete the code to access the route parameter named 'postId'.
Express
app.get('/post/:postId', (req, res) => { const postId = req.params.[1]; res.send(`Post ID: ${postId}`); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than defined in the route
Trying to access
req.params.id when the parameter is named postId✗ Incorrect
You access the route parameter by its exact name, here postId.
3fill in blank
hardFix the error in the route to correctly capture the 'category' parameter.
Express
app.get('/items/[1]', (req, res) => { res.send(`Category: ${req.params.category}`); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the colon before the parameter name
Using wildcard or optional parameter syntax incorrectly
✗ Incorrect
The route parameter must start with a colon (:), so use ':category'.
4fill in blank
hardFill both blanks to define a route with two parameters and access them.
Express
app.get('/[1]/:[2]', (req, res) => { res.send(`Category: ${req.params.category}, ID: ${req.params.id}`); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using parameter syntax for the first segment
Mixing up parameter names
✗ Incorrect
The route path should be '/category/:id' to capture both parameters correctly.
5fill in blank
hardFill all three blanks to create a route with nested parameters and access them.
Express
app.get('/[1]/:[2]/[3]/:commentId', (req, res) => { res.send(`Post: ${req.params.postId}, Comment: ${req.params.commentId}`); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using singular instead of plural fixed segments
Forgetting the colon before parameter names
✗ Incorrect
The route path should be '/posts/:postId/comments/:commentId' to capture both post and comment IDs.