0
0
Expressframework~10 mins

req.params for route parameters in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to access the route parameter 'id' using req.params.

Express
app.get('/user/:id', (req, res) => {
  const userId = req.params.[1];
  res.send(`User ID is ${userId}`);
});
Drag options to blanks, or click blank then click option'
Aid
Bparams
Cquery
Dbody
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.query instead of req.params
Trying to access req.body for route parameters
2fill in blank
medium

Complete the code to define a route that captures a 'postId' parameter.

Express
app.get('/posts/:[1]', (req, res) => {
  res.send(`Post ID: ${req.params.postId}`);
});
Drag options to blanks, or click blank then click option'
ApostId
Bid
Cpost
Dpid
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name in the route and in req.params
Omitting the ':' before the parameter name
3fill in blank
hard

Fix the error in accessing the 'category' parameter from the route.

Express
app.get('/items/:category', (req, res) => {
  const cat = req.[1].category;
  res.send(`Category: ${cat}`);
});
Drag options to blanks, or click blank then click option'
Abody
Broute
Cparams
Dquery
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.body or req.query to get route parameters
Misspelling 'params'
4fill in blank
hard

Fill both blanks to create a route that captures 'userId' and 'postId' parameters and sends them back.

Express
app.get('/users/:[1]/posts/:[2]', (req, res) => {
  res.send(`User: ${req.params.userId}, Post: ${req.params.postId}`);
});
Drag options to blanks, or click blank then click option'
AuserId
BpostId
Cid
Dpid
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names in the route and in req.params
Forgetting the colon ':' before parameter names
5fill in blank
hard

Fill all three blanks to create a route with parameters 'category', 'id', and 'action' and send a response using them.

Express
app.get('/:[1]/:[2]/:[3]', (req, res) => {
  res.send(`Category: ${req.params.category}, ID: ${req.params.id}, Action: ${req.params.action}`);
});
Drag options to blanks, or click blank then click option'
Acategory
Bid
Caction
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using parameter names inconsistently
Missing colons before parameter names