Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
147
rated 0 times [  149] [ 2]  / answers: 1 / hits: 15924  / 9 Years ago, fri, january 1, 2016, 12:00:00

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 www.site.com/users/bob/ instead of www.site.com/users/bob


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!


edit:


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


More From » node.js

 Answers
6

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.


[#63876] Tuesday, December 29, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
estefanib

Total Points: 508
Total Questions: 104
Total Answers: 83

Location: Lebanon
Member since Sun, Aug 2, 2020
4 Years ago
;