Express router ignores strict option and incorrectly reports req.baseUrl
See original GitHub issueUsing this setup:
app.set("strict routing", true);
let router_x = express.Router({strict: true});
router_x.use("/j/1", index_get);
router_x.use("/j", index_get);
router_x.use(not_found);
app.use("/x", router_x);
app.get("/a/1", index_get);
app.get("/a", index_get);
app.get("/", index_get);
app.use(not_found);
and requesting /x/j/1
, reports req.baseUrl
as /x/j/1
and req.path
as /
. I expect the base URL reported as the mount point for the router instance, which is /x
in this case, but instead it just uses the current route as the base URL.
Moreover, the router is configured with strict: true
, but it accepts any additional path segments. For example, I can request /x/j/3/2/1
and it will report req.baseUrl
as /x/j
and req.path
as /3/2/1
.
The same logic against the application router in the example above works as expected, so requesting /a/
and /a/1/
both return 404
and /a
and /a/1
both work properly.
I found this issue about the strict option not being respected if a router is configured with the /
path, but this is not the case here, so it seems to be a different issue.
https://github.com/expressjs/express/issues/2281
Here’s a more complete sample that shows strict routing working properly at the application level and two routers, one with strict routing and one without, both working the same way.
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (4 by maintainers)
Top GitHub Comments
That looks great 👍
@dougwilson Maybe something along these lines would make it more visible that routers don’t inherit routing options from the application when they are mounted and instead retain options passed into
express.Router
.https://github.com/gh-andre/expressjs.com/commit/246e973cc7de10fe09507e3269603dee22230739