Route Parameters
Swipe to show menu
Sometimes you need dynamic routes that change based on input, such as an ID.
Express allows you to define route parameters using :.
app.get('/users/:id', (req, res) => {
const userId = req.params.id;
res.send(`User ID is ${userId}`);
});
Here, :id is a dynamic value. When a request comes in, Express extracts it and makes it available in req.params.
For example:
'/users/1': id = 1;'/users/42': id = 42.
This is commonly used to get specific data, such as a user by ID or a product by ID.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Route Parameters
Sometimes you need dynamic routes that change based on input, such as an ID.
Express allows you to define route parameters using :.
app.get('/users/:id', (req, res) => {
const userId = req.params.id;
res.send(`User ID is ${userId}`);
});
Here, :id is a dynamic value. When a request comes in, Express extracts it and makes it available in req.params.
For example:
'/users/1': id = 1;'/users/42': id = 42.
This is commonly used to get specific data, such as a user by ID or a product by ID.
Thanks for your feedback!