gpt4 book ai didi

javascript - 函数末尾的这些 Node js 变量是什么?

转载 作者:太空宇宙 更新时间:2023-11-04 01:02:46 25 4
gpt4 key购买 nike

我正在使用 NodeJS、Express 和 PassportJS 创建一个 API,但我认为这是一个 JavaScript 问题。

app.get('/test', function (req, res, next) {
passport.authenticate('bearer', { session: false },

function (err, user, info) {
if (user === false) {
res.send('ko');
} else {
res.send('ok');
}
})(req, res, next);
});

我的问题是:

为什么在authenticate函数之后是(req, res, next)?与范围有关吗?

最佳答案

似乎函数password.authenticate返回一个函数/闭包。代码就像

foo(x, y)(z);

即调用 foo(x, y) 返回的函数使用参数 z 进行调用。

一个非常简单的例子是

function multiplier(k) {
return function(x) { return x*k; };
}

console.log(multiplier(7)(6)); // outputs 42

关于javascript - 函数末尾的这些 Node js 变量是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25698660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com