底部的 `(req,res,next)` 是什么意思?

What does `(req,res,next)` at bottom mean?

router.post('/login', isNotLoggedIn, (req, res, next) => {
    passport.authenticate('local', (authError, user, info) => {
        if (authError) {
            console.error(authError);
            return next(authError);
        }
        if (!user) {
            req.flash('loginError', info.message);
            return res.redirect('/');
        }

        return req.login(user, (loginError) => {
            if (loginError) {
                console.error(loginError);
                return next(loginError);
            }

            return res.redirect('/');
        });
    })(req, res, next);// <-- this line
});

我正在尝试通过在线讲座学习护照包。但是,我没有在底部得到这个 (req,res,next)。谁能帮我看看这是什么意思?

根据 Passport Doc:

authenticate()'s function signature is standard Connect middleware, which makes it convenient to use as route middleware in Express applications.

所以,基本上连接中间件有这种类型的结构:

function middleware(req, res, next){
   //do something
}

您可以从express.js docs.

中查看什么是连接类型中间件

但最终,passport.authenticate() returns 一个与上述签名相同的函数。

所以当你

passport.authenticate(.....)(req, res, next)

您正在这样做:

(function(req, res, next){
   //do something
})(req, res, next)

你基本上是将 express.request 作为 req 传递,express.response 作为 res 传递,而 next 是指向下一个处理程序的指针。

(...) 在任何函数声明之后是标准执行运算符:

const fn = function test() {
  return 'test';
}

将导致 fn 成为函数。

const fn = (function test() {
  return 'test';
})();

将导致 fn 成为字符串 test 因为声明的函数在 赋值发生之前得到 运行 ,类似于:

const tempfunction = function() {
   return 'test';
};

const fn = tempFunction();

但没有中介 tempFunction 徘徊。

在这种情况下,您显示的代码在功能上等同于此:

router.post("/login", isNotLoggedIn, (req, res, next) => {
  // declare a passport authentication handler
  const authFn = (authError, user, info) => {
    if (authError) {
      console.error(authError);
      return next(authError);
    }
    if (!user) {
      req.flash("loginError", info.message);
      return res.redirect("/");
    }

    return req.login(user, loginError => {
      if (loginError) {
        console.error(loginError);
        return next(loginError);
      }

      return res.redirect("/");
    });
  };

  // call passport to generate an express middleware function
  const passportMiddleware = passport.authenticate("local", authFn);

  // run
  passportMiddleware(req, res, next);
});