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
✗ Incorrect
Route parameters are defined with a colon before the name, like '/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
✗ Incorrect
Route parameters are stored in 'req.params' as key-value pairs.
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
✗ Incorrect
Adding '?' after a parameter name makes it 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
✗ Incorrect
Route parameters are accessed via 'req.params' by their names.
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?
✗ Incorrect
Without a colon, 'itemId' is treated as a fixed path segment, not a parameter.
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.
Practice
(1/5)
1. What does req.params represent in an Express route handler?
easy
A. An object containing route parameter values from the URL
B. A function to send responses to the client
C. A middleware to parse JSON bodies
D. A method to define static file paths
Solution
Step 1: Understand route parameters
Route parameters are parts of the URL defined with a colon, like /user/:id.
Step 2: Accessing parameters in Express
Express stores these dynamic values in req.params as an object with keys matching parameter names.
Final Answer:
An object containing route parameter values from the URL -> Option A
Quick Check:
req.params = route parameters object [OK]
Hint: Remember: req.params holds URL dynamic parts [OK]
Common Mistakes:
Confusing req.params with req.body
Thinking req.params is a function
Using req.query instead of req.params
2. Which of the following is the correct way to define a route with a parameter named postId in Express?
easy
A. app.get('/post/:postId', (req, res) => { ... })
B. app.get('/post/postId', (req, res) => { ... })
C. app.get('/post/?postId', (req, res) => { ... })
D. app.get('/post/*postId', (req, res) => { ... })
Solution
Step 1: Syntax for route parameters
Express uses a colon : before the parameter name to mark it as dynamic.
Step 2: Check each option
app.get('/post/:postId', (req, res) => { ... }) uses /:postId correctly. Options A, B, and D use incorrect syntax for parameters.
Final Answer:
app.get('/post/:postId', (req, res) => { ... }) -> Option A
Quick Check:
Colon before param name = correct route param syntax [OK]
Hint: Use colon before param name in route path [OK]
Common Mistakes:
Omitting the colon before parameter name
Using query syntax instead of route params
Using wildcard * incorrectly for parameters
3. What will be the output of the following Express route handler when a GET request is made to /user/42?
app.get('/user/:id', (req, res) => {
res.send(`User ID is ${req.params.id}`);
});
medium
A. User ID is undefined
B. User ID is 42
C. User ID is :id
D. Error: Cannot read property 'id' of undefined
Solution
Step 1: Identify the route parameter
The route defines a parameter :id, so req.params.id will hold the value from the URL.
Step 2: Check the request URL and response
The URL /user/42 sets id to '42', so the response sends 'User ID is 42'.
Final Answer:
User ID is 42 -> Option B
Quick Check:
req.params.id = '42' from URL [OK]
Hint: Route param value replaces :id in req.params [OK]
B. The route path is missing a colon before productId
C. res.send cannot send strings
D. The route parameter name and req.params key do not match
Solution
Step 1: Check route parameter name
The route defines parameter productId, so req.params will have a key named productId.
Step 2: Check usage of req.params
The code uses req.params.product, which does not exist, so id will be undefined.
Final Answer:
The route parameter name and req.params key do not match -> Option D
Quick Check:
Param name mismatch causes undefined value [OK]
Hint: Match param name exactly in req.params key [OK]
Common Mistakes:
Using wrong key name from req.params
Forgetting colon in route path
Assuming req.params is always empty
5. You want to create an Express route that handles URLs like /order/123/item/456 to get both orderId and itemId. Which route definition and parameter access is correct?