0
0
Expressframework~5 mins

Route parameters in Express - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What are route parameters in Express?
Route parameters are parts of the URL that act as placeholders for values. They let you capture values from the URL and use them in your code.
Click to reveal answer
beginner
How do you define a route parameter in an Express route?
You define a route parameter by adding a colon (:) before a name in the route path, like '/user/:id'. This means 'id' is a parameter you can access.
Click to reveal answer
beginner
How do you access route parameters inside an Express route handler?
You access route parameters using 'req.params'. For example, if your route is '/user/:id', you get the id with 'req.params.id'.
Click to reveal answer
intermediate
What happens if you define multiple route parameters in Express?
You can define multiple parameters like '/user/:userId/book/:bookId'. Each parameter is available in 'req.params' by its name, e.g., 'req.params.userId' and 'req.params.bookId'.
Click to reveal answer
intermediate
Can route parameters be optional in Express? How?
Yes, by adding a question mark (?) after the parameter name, like '/user/:id?'. This means the parameter is optional and the route matches with or without it.
Click to reveal answer
How do you define a route parameter named 'postId' in Express?
A/post/?postId
B/post/postId
C/post/:postId
D/post/*postId
Where do you find the value of a route parameter inside an Express route handler?
Areq.body
Breq.params
Creq.query
Dreq.headers
What does the route '/user/:id?' mean in Express?
AThe 'id' parameter is required
BThe route matches only if 'id' is a string
CThe route matches only if 'id' is a number
DThe 'id' parameter is optional
If you have a route '/product/:category/:id', how do you access the 'category' parameter?
Areq.params.category
Breq.query.category
Creq.body.category
Dreq.headers.category
Which of these is NOT a valid route parameter definition in Express?
A/item/itemId
B/item/:itemId/details
C/item/:itemId
D/item/:itemId?
Explain how to create and use route parameters in Express routes.
Think about how URLs can carry data like user IDs.
You got /3 concepts.
    Describe how to make a route parameter optional and why you might want to do that.
    Optional means the URL can have that part or skip it.
    You got /3 concepts.