如何访问node.js中的req.host

How to access req.host in node.js?

本文关键字:js 中的 req host node 何访问 访问      更新时间:2023-09-26

我是node.js世界的新手。我使用passportjs库来使用api密钥对用户进行身份验证。我想做的是将其与api密钥一起使用。我还想检查请求的主机名。

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

我不知道passportjs是如何调用下面的函数的。但它肯定会在post请求到达路径"/api/authenticate"后调用该函数。我还想访问下面函数中的req.host。

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

有可能吗?如对此有任何见解,我们将不胜感激。非常感谢。

使用passReqToCallback选项。详见本页底部:

你必须这样更新你的代码:

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

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