简体   繁体   English

如何在node.js中访问req.host?

[英]How to access req.host in node.js?

I am newbie to the node.js world. 我是node.js世界的新手。 I am using passportjs library for authenticating the user with api key. 我正在使用Passportjs库通过api密钥对用户进行身份验证。 What I am trying to do is that along with api key. 我想做的是与api键一起使用。 I also want to check the host name of the request. 我也想检查请求的主机名。

app.post('/api/authenticate', 
  passport.authenticate('localapikey'),//passport module method to authenticate the api key
  function(req, res) {
    console.log('Authenticated');
  });

I don't know how passportjs calling the below function. 我不知道passportjs如何调用以下功能。 But it definitely calling the function after a post request is coming to the path '/api/authenticate'. 但是它肯定会在发布请求到达路径“ / api / authenticate”之后调用该函数。 I also want to access the req.host in the below function. 我还想在下面的函数中访问req.host。

passport.use(new LocalStrategy(
  function(apikey, done) {
    console.log(req.host);
}

Is it possible? 可能吗? Any insight into this would highly be appreciated. 任何对此的见解将不胜感激。 Thank you. 谢谢。

Use the passReqToCallback option. 使用passReqToCallback选项。 See the bottom of this page for details: 有关详细信息,请参见此页面底部:

You must update your code like this: 您必须像这样更新代码:

passport.use(new LocalStrategy({
    passReqToCallback: true
  },
  function(req, apikey, done) {
      console.log(req.host);
  }
));

http://passportjs.org/guide/authorize/ http://passportjs.org/guide/authorize/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM