Enforce URL shape
An API needs canonical URL forms and should reject requests that add or omit a trailing slash.
Express.js · Const
express.Router({ strict: true }) makes trailing slashes significant when matching routes. By default, /foo and /foo/ are treated as the same path.
Pass strict: true to express.Router() to make trailing slashes significant for routes managed by that router. With the default setting, a path such as /events also matches /events/; strict routing separates those two forms.
express.Router([options])| Name | Type | Description |
|---|---|---|
options | Object | Optional router configuration object; its strict Boolean controls whether trailing slashes distinguish paths. |
Router
false.true enables strict route matching.const express = require('express');
const app = express();
const router = express.Router({ strict: true });
router.get('/events', (req, res) => res.send('events'));
app.use('/calendar', router);
app.listen(3000);Creates a router where /events and /events/ are distinct paths, then mounts it under /calendar.
An API needs canonical URL forms and should reject requests that add or omit a trailing slash.
A mounted feature router must use stricter path matching without changing unrelated routers.
Two endpoints intentionally differ only by whether the path ends in a slash.
Set the router option to strict: true, for example express.Router({ strict: true }). This makes /foo and /foo/ different paths.
Follow-up: What happens when the option is omitted? The router defaults to non-strict behavior, treating both forms as equivalent.
strict option?The option is Boolean and defaults to false. Therefore, a router normally treats /foo and /foo/ as the same route.
Follow-up: How would you make only one router strict? Create that router with strict: true; other routers can retain their own options.
Use express.Router({ strict: true }), add routes to the returned router, and mount it with app.use(). The router remains middleware-compatible while applying its route-matching policy.
Follow-up: Can strict routing be combined with case sensitivity? Yes; pass both options, such as { caseSensitive: true, strict: true }.
express.Router()caseSensitiverouter.get()app.use()