Read a resource identifier
A route such as /users/:userId needs to identify the requested user.
Express.js · Const
req.params is an object containing values captured by named parameters in the matched Express route. Named captures use parameter-name properties, while regular-expression captures can use numeric keys.
Express fills req.params with values captured by named segments in the matched route path. The property is an object whose keys are parameter names; regular-expression routes can additionally expose capture groups through numeric keys.
req.paramsAn object containing values captured from the matched route's parameters.
const express = require('express');
const app = express();
app.get('/users/:userId/books/:bookId', (req, res) => {
res.json({ userId: req.params.userId, bookId: req.params.bookId });
});
app.listen(3000);This route reads both named URL segments from req.params and returns them as JSON.
A route such as /users/:userId needs to identify the requested user.
A nested route captures multiple URL segments such as /users/:userId/books/:bookId.
A route uses a regular expression with capture groups.
Express populates req.params from named parameters in the matched route path. For /user/:name, the captured value is available as req.params.name.
Follow-up: How are unnamed regular-expression captures accessed? They are exposed with integer keys such as req.params[0], where the index identifies the capture group.
It is an object, and with string route paths its default value is {}. Each named route parameter becomes a property whose value is the corresponding captured URL segment.
Follow-up: What happens when a route uses a regular expression? The object remains standard and capture groups are available through numeric keys.
Use app.param when parameter-specific processing or changes are needed. Such changes apply only to parameters that the route path already defines.
Follow-up: Can app.param add an unrelated key to req.params? No; the documented behavior applies only to parameters already defined in the route path.
app.paramreq.queryreq.bodyreq.route