how to get request path with express req object

chovy picture chovy · Sep 21, 2012 · Viewed 160.8k times · Source

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

Menztrual picture Menztrual · Sep 21, 2012

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

console.log(req.originalUrl)