试图理解为什么在通话结束时会有额外的括号

Trying to understand why there are extra brackets at the end of a call

这可能是一个愚蠢的问题,我正在尝试通过 OAuth 的示例并希望在添加到我自己的代码之前准确了解发生了什么。

样本是 nodeexpress 使用 passport-azure-ad

正在定义路由并调用 passport.authenticate

app.get('/login',
  (req, res, next) => {
      passport.authenticate('azuread-openidconnect', 
      { 
        response: res,
        resourceURL: config.resourceURL,
        failureRedirect: '/' 
      })(req, res, next); // <-- Here is what I am stuck on. 
   },
   (req, res) => {
       log.info('Login was called in the Sample');
       res.redirect('/');
});

我试图理解身份验证后紧随其后的 (req, res, next);

感谢任何帮助,或对 theory/documentation 语法的 link。

那是因为 passport.authenticate returns 一个处理请求的函数(中间件),所以你在这里将请求传递给实际的处理程序

像这样:

function authenticate(someArg) {
    return function (req, res, next) {
        // the handler
    }
}

这是您提供的示例的简化版本,没有额外显式传递参数

app.get('/login', passport.authenticate('azuread-openidconnect', { 
    response: res,
    resourceURL: config.resourceURL,
    failureRedirect: '/' 
}), (req, res) => {
    log.info('Login was called in the Sample');
    res.redirect('/');
});

我认为这只是理解所谓 "lambda" 函数的 Javascript 语法的问题。考虑以下表达式:

(a) => { console.log(a) }

这是一种编写接受一个参数并将其打印出来的函数的方法。您可以将该表达式放在任何您需要指定打印出一个参数的函数的地方。这很有用,因为在 Javascript 中,函数可以像数据一样传递,并且这种语法允许您在需要时立即定义函数,而无需为它命名。

在您的示例中,您使用三个参数调用 app.get。第一个是字符串“/login”。第二个是一个带 3 个参数的函数,函数就在那里定义,调用 passport.authenticate,returns 一个函数,用这 3 个参数调用。第三个是一个有两个参数的函数,也在行中定义。