expressjs / expressjs/express

'/' route breaks strict routing

Open
#2,281 33 comments 14 reactions 0 assignees View on GitHub
5.x bug module:router
Dominant language
JavaScript
Stars
69.5k
Forks
25k
Avg merge
4d 20h
Merged PRs (30d)
9

Description

Hi there,

Currently, if you define a route like this:

``` javascript
route = require('express').Router({strict: true});
route.get('/', function(req, res) {
res.send('hi');
});
```

And `use()` that in an express application like this:

``` javascript
app = require('express')();
app.use('/strict/', route);
```

You will receive a 200 when requesting `/strict/` and `/strict`. I would expect only `/strict/` to return a 200.

I've found an acceptable workaround by adding a check like this:

``` javascript
route.get('/', function(req, res, next) {
if (req.originalUrl.slice(-1) != '/') return next();
res.send('root with slash');
});
route.get('/', function(req, res) {
res.send('root without slash');
});
```

But I think it would be less surprising if `route.get('/')` only worked for the path ending in `/` when strict routing is enabled, and perhaps `route.get('')` could be used for the no-slash case.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.