Redirection Node.js with / without slash


I have a javascript that does different things depending on the URL. for that to work, i need to have consistent URIs.

for example, i need users to always be on instead of

node unfortunately doesn't support that, as it seems.

i tried redirecting with

router.get('/:user', function(req, res) {
    res.redirect('/users/' + req.params.user' + '/');

but it just results in a redirect loop, as the URL with and without slash seem to be treated as the same.

how can i do this? thanks!


i want to route from WITHOUT slash to WITH slash. the answers in the other question address the other way around. i can't .substr(-1) my URLs

You can get this by using third-party library which is called express-slash . All you need to do is,

First, install the library

$ npm install express-slash

And, add these to your app.js.

var slash   = require('express-slash');

app.use(slash()); // set slash middleware

Then, this is your router.

router.get('/:user/', function(req, res) {
    // do your stuff

Hope it will be useful for you.