Route parameters let you capture parts of a URL as variables. This helps you handle dynamic content like user profiles or product pages.
Route parameters in Express
app.get('/path/:parameterName', (req, res) => { const param = req.params.parameterName; res.send(`Value is ${param}`); });
Route parameters start with a colon : in the route path.
You access the parameter value inside the handler with req.params.parameterName.
id from the URL like /user/123.app.get('/user/:id', (req, res) => { res.send(`User ID is ${req.params.id}`); });
category and id.app.get('/product/:category/:id', (req, res) => { res.send(`Category: ${req.params.category}, Product ID: ${req.params.id}`); });
app.get('/post/:postId/comment/:commentId', (req, res) => { res.send(`Post: ${req.params.postId}, Comment: ${req.params.commentId}`); });
This simple Express app listens on port 3000. It has one route that takes a userId from the URL and sends it back in the response.
Try visiting http://localhost:3000/user/42 in your browser to see the output.
import express from 'express'; const app = express(); const port = 3000; app.get('/user/:userId', (req, res) => { const userId = req.params.userId; res.send(`User ID received: ${userId}`); }); app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); });
Route parameters only capture the part of the URL where you put the : placeholder.
If you want to accept optional parameters, you can add a question mark like :param?.
Always validate or sanitize route parameters before using them to avoid security issues.
Route parameters let you get dynamic values from the URL.
Use req.params to access these values inside your route handler.
This helps build flexible routes for things like user profiles, products, or posts.
Practice
req.params represent in an Express route handler?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 inreq.paramsas an object with keys matching parameter names.Final Answer:
An object containing route parameter values from the URL -> Option AQuick Check:
req.params = route parameters object [OK]
- Confusing req.params with req.body
- Thinking req.params is a function
- Using req.query instead of req.params
postId in Express?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/:postIdcorrectly. Options A, B, and D use incorrect syntax for parameters.Final Answer:
app.get('/post/:postId', (req, res) => { ... }) -> Option AQuick Check:
Colon before param name = correct route param syntax [OK]
- Omitting the colon before parameter name
- Using query syntax instead of route params
- Using wildcard * incorrectly for parameters
/user/42?
app.get('/user/:id', (req, res) => {
res.send(`User ID is ${req.params.id}`);
});Solution
Step 1: Identify the route parameter
The route defines a parameter:id, soreq.params.idwill hold the value from the URL.Step 2: Check the request URL and response
The URL/user/42setsidto '42', so the response sends 'User ID is 42'.Final Answer:
User ID is 42 -> Option BQuick Check:
req.params.id = '42' from URL [OK]
- Expecting req.params to be undefined
- Using req.query instead of req.params
- Forgetting to use backticks for template literals
app.get('/product/:productId', (req, res) => {
const id = req.params.product;
res.send(`Product ID: ${id}`);
});
What is the problem with this code?Solution
Step 1: Check route parameter name
The route defines parameterproductId, soreq.paramswill have a key namedproductId.Step 2: Check usage of req.params
The code usesreq.params.product, which does not exist, soidwill be undefined.Final Answer:
The route parameter name and req.params key do not match -> Option DQuick Check:
Param name mismatch causes undefined value [OK]
- Using wrong key name from req.params
- Forgetting colon in route path
- Assuming req.params is always empty
/order/123/item/456 to get both orderId and itemId. Which route definition and parameter access is correct?Solution
Step 1: Define route with two parameters
The route must have:orderIdand:itemIdto capture both values from the URL.Step 2: Access parameters correctly
Usereq.params.orderIdandreq.params.itemIdto get the values inside the handler.Final Answer:
app.get('/order/:orderId/item/:itemId', (req, res) => { res.send(`Order: ${req.params.orderId}, Item: ${req.params.itemId}`); }) -> Option CQuick Check:
Route params and req.params keys must match [OK]
- Mixing req.params with req.query
- Using wrong parameter names in handler
- Defining route without all needed parameters
Ready to prove it?
Pass the test and this pattern moves unknown
