chovy chovy - 3 months ago 37
Javascript Question

how to get request path with express req object

I'm using express + node.js and I have a req object, the request in the browser is /account but when I log req.path I get '/' --- not '/account'.

//auth required or redirect
app.use('/account', function(req, res, next) {
console.log(req.path);
if ( !req.session.user ) {
res.redirect('/login?ref='+req.path);
} else {
next();
}
});


req.path is / when it should be /account ??

Answer

After having a bit of a play myself, you should use:

console.log(req.originalUrl)

Comments