I am creating an API with NodeJS, Express and PassportJS but I think this is a JavaScript question.
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);
});
My question is:
Why is (req, res, next)
after the authenticate function? Is it related with the scope?
Seems that the function password.authenticate
returns a function/closure. The code is like
foo(x, y)(z);
i.e. the function returned by the call foo(x, y)
is called with parameter z
.
A very simple example is
function multiplier(k) {
return function(x) { return x*k; };
}
console.log(multiplier(7)(6)); // outputs 42
The ()
call the function. The variables inside it are passed to it as arguments. You can see them coming into the containing function on line one of your code.