Express: case sensitive routing not working

Created on 19 Jan 2015  路  9Comments  路  Source: expressjs/express

When I write app.set('case sensitive routing', true);, the server still works when receiving requests for /Path and /path even when the route is router.get('/path/:value([a-z]+)', router_handler);. Also I'd like value only allows lower letters, but it receives upper letters. What am I doing wrong?.

question

Most helpful comment

I made it work using:

router = express.Router({
  caseSensitive: true
});

instead of enabling case sensitive routing on app.

All 9 comments

I made it work using:

router = express.Router({
  caseSensitive: true
});

instead of enabling case sensitive routing on app.

That was quick :+1:

As for the app.set('case sensitive routing', true); issue; I would need to see a full piece of code to reproduce, but most likely what you are seeing is that most core settings like "case sensitive routing" need to be set before your first app.use(), or first route, otherwise they have no effect.

Will write a simple piece of code, it will help me to check if I have something wrong on my big project or this is a :bug: because it doesn't work for me even when I set the option before my app.use and my route declarations.

Cool :) I can re-open based on your findings! For reference, the only test we have for this is https://github.com/strongloop/express/blob/4.11.0/test/app.router.js#L212-L240

I wrote this POC, please check route.coffee@line5 and app.coffee@line9.

As you can see I'm working with a route in a different file, obviously this was my fault because I was expecting case sensitive will work globally, but we need to enable caseSensitive on the router.

Then we have:

Request /some/path/abc will work.
Request /Some/path/abc will not work because is defined on app.
Request /some/Path/abc will work because caseSensitive is not true in the Router definition.
Request /some/path/abC will work because caseSensitive is not true in the Router definition.

It doesn't look as a :bug: even when the regex on the path definition for value only allow lower letters.

Ok, I understand now :) So yes, with Express 4, when you do express.Router() in a different file, it doesn't have the same settings as the app's router (the case sensitive routing setting, in this case). I was actually thinking as part of Express 5 to add a app.createRouter(opts) that would default the options to the same as the app itself, allowing you to keep the settings downward, though it wouldn't really help in your case.

Each router instance is basically intended to be isolated from each other, to promote code re-use (because, well, it's hard to re-use a router in an app that decides to change the routing semantics :). like if you had your router and it expected case sensitive routes, but then you tried to re-use your router an an app that did not have case sensitive routes and it couldn't be changed for legacy reasons, etc.).

It makes all sense and I totally agree :+1:

Thanks a lot

No problem :) I'm happy to help.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ZeddYu picture ZeddYu  路  3Comments

prashantLio picture prashantLio  路  3Comments

Domiii picture Domiii  路  3Comments

snowdream picture snowdream  路  3Comments

zackarychapple picture zackarychapple  路  3Comments