如何使用express req对象获取请求路径 [英] how to get request path with express req object

查看:309
本文介绍了如何使用express req对象获取请求路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用express + node.js,我有一个req对象,浏览器中的请求是/ account,但是当我记录req.path时,我得到'/'---不是'/account'.

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是/当它应该是/ account?

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

推荐答案

自己玩一下,你应该使用:

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

console.log(req.originalUrl)

这篇关于如何使用express req对象获取请求路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆