question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Express router ignores strict option and incorrectly reports req.baseUrl

See original GitHub issue

Using 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.

express-route-opts-bug.zip

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dougwilsoncommented, Feb 23, 2022

That looks great 👍

0reactions
gh-andrecommented, Feb 23, 2022

@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

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - Express app - Change base url - Stack Overflow
I tried this and got this error: /var/www/public/citizenpedia/node_modules/express/lib/router/index.js:458 throw new TypeError('Router. · I have ...
Read more >
5.x API - Express.js
strict, Enables or disables only accepting arrays and objects; ... If a function, the type option is called as fn(req) and the request...
Read more >
Express.js req.baseUrl Property - GeeksforGeeks
The req.baseUrl property is the URL path on which a router instance was mounted. The req.baseUrl property is similar to the mount path ......
Read more >
Node.js and Express Tutorial: Authentication Using Passport
It's the base URL you use to access the Auth0 API and the URL where your application redirects users for Auth0 to authenticate...
Read more >
client/node_modules/express/History.md - GitLab
Set stricter CSP header in redirect response ... Fix false detection of no-cache request directive; Fix incorrect result when If-None-Match ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found