Express req.params Property
What you’ll learn
- How to read dynamic route values from
req.params. - How to work with single and multiple path parameters.
- How to validate and convert parameter values safely.
- How
req.paramsdiffers fromreq.query.
Usage syntax
javascript
req.params
req.params.id
req.params.userId1
Read single route parameter
javascript
app.get('/users/:id', function (req, res) {
res.send('User ID: ' + req.params.id);
});2
Use multiple route params
javascript
app.get('/users/:userId/posts/:postId', function (req, res) {
var userId = req.params.userId;
var postId = req.params.postId;
res.json({ userId: userId, postId: postId });
});❓ FAQ
It is an object containing named route parameters extracted from the URL path.
They come from route patterns like /users/:id or /posts/:postId/comments/:commentId.
Yes, they are usually strings and should be validated/converted when numeric values are expected.
req.params comes from route path segments, while req.query comes from the URL query string.
Yes, with middleware or app.param() you can validate and normalize param values.
Did you know?
req.params contains key-value pairs from named route segments like /users/:id.
4 people found this page helpful
