Nested resource routes
A mounted child router must read an identifier captured by its parent path.
Express.js · Interface
RouterOptions is the optional configuration object for express.Router(). It controls case sensitivity, parent-parameter inheritance, and strict trailing-slash matching; each option defaults to false.
RouterOptions is the optional object accepted by express.Router([options]). Its three documented settings control URL case matching, inheritance of parent route parameters, and treatment of trailing slashes; omitted settings default to false.
express.Router([options])| Name | Type | Description |
|---|---|---|
options | Object | undefined | Optional configuration object for router behavior; omitted properties default to false. |
options.caseSensitive | Boolean | Enables case-sensitive matching; default false. |
options.mergeParams | Boolean | Preserves parent-router req.params; default false. |
options.strict | Boolean | Enables strict routing for trailing slashes; default false. |
Router — a router object to which middleware and HTTP method routes can be added.
const express = require('express');
const router = express.Router({
caseSensitive: true,
mergeParams: true,
strict: true
});
router.get('/users/:id', (req, res) => {
res.json({ parentId: req.params.parentId, id: req.params.id });
});
const app = express();
app.use('/teams/:parentId', router);
app.listen(3000);Creates a router that distinguishes URL case and trailing slashes while retaining parameters inherited from its mounted parent route.
A mounted child router must read an identifier captured by its parent path.
An API requires URLs whose capitalization carries meaning.
An application must distinguish canonical paths with and without trailing slashes.
RouterOptions is the optional configuration object passed to express.Router. It supports caseSensitive, mergeParams, and strict, and the router is returned for adding middleware and routes.
Follow-up: How would a mounted router access a parameter from its parent path? Pass mergeParams: true when constructing the router; otherwise the parent parameter is not preserved.
caseSensitive enables case-sensitive path matching. Its default is false, so paths such as /Foo and /foo are treated as equivalent.
Follow-up: What effect does strict have instead? It controls trailing-slash matching, making /foo and /foo/ distinct when enabled.
mergeParams preserves req.params values from the parent router when a child router is mounted. If names conflict, the child parameter takes precedence.
Follow-up: When is this useful? It is useful for nested routes whose handlers need an identifier captured by the mounting path.
express.Router()Routerreq.paramsrouter.use()router.get()