Express: Double slash before Router (router//route)

Created on 9 Oct 2020  路  5Comments  路  Source: expressjs/express

Hello, when i assign a router to the route, then there is access to routes with a double slash at the beginning.

example:

const express = require('express');
const app = express();
const port = 3000;

const router = express.Router({ strict: true }); // i've tried without strict mode
router.get('/test', (req, res) => {
  res.send('test');
});

app.use('/user', router);

app.listen(port, () => console.log(`app listening on port ${port}!`))

The router is supposed to run on the /user/test route, but the /user//test also works.

Environment:

"node": 14.3.0
"express": 4.17.1
question

All 5 comments

Consecutive multiple path separators (the forward slash, in your case) in the URLs do not have any significance in the modern-day operating systems. But it is advised not to use like so.

Please refer to the Stackoverflow question here - https://stackoverflow.com/questions/10161177/url-with-multiple-forward-slashes-does-it-break-anything.

Consecutive multiple path separators (the forward slash, in your case) in the URLs do not have any significance in the modern-day operating systems. But it is advised not to use like so.

Please refer to the Stackoverflow question here - https://stackoverflow.com/questions/10161177/url-with-multiple-forward-slashes-does-it-break-anything.

yes, but it's not expected behavior, I'm interested in how to disallow double slashes

@checnev usually this situation is not good for SEO purposes, I assume it's possible to write a middleware to detect this case and return 301 redirect to correct URI.

Consecutive multiple path separators (the forward slash, in your case) in the URLs do not have any significance in the modern-day operating systems. But it is advised not to use like so.
Please refer to the Stackoverflow question here - https://stackoverflow.com/questions/10161177/url-with-multiple-forward-slashes-does-it-break-anything.

yes, but it's not expected behavior, I'm interested in how to disallow double slashes

But is it is advised to handle it by writing a middleware as double slashes may cause problems while caching.

I'm closing this as I truly believe in case this behaviour causes issues - it can be easily handled on middleware level. Please feel free to reopen in case I'm mistaken and we will try to raise it further to maintainers.

Was this page helpful?
0 / 5 - 0 ratings