护照的身份验证功能如何知道要验证哪个请求?

How does passport's authenticate function know which request to authenticate?

passport docs用这个来保护路由:

app.get('/api/me',
  passport.authenticate('basic', { session: false }),
  function(req, res) {
    res.json(req.user);
  });

authenticate() 如何知道要验证哪个请求?我没有将请求传递给它。

passport.authenticatereturns一个函数。

你可以试试这个

console.log(passport.authenticate('basic', { session: false }));

它会打印类似

的内容
function(req, res, next){ ... }

这意味着 app.get 在您的应用程序启动后看起来像这样

app.get('/api/me',
    function(req, res, next){
         // now passport has access to the "req" even though you didn't pass request to it
         // passport's authentication logic here
    },
    function(req, res) {
       res.json(req.user);
    });