Why does the following middleware syntax not work for Express?
-
06-06-2021 - |
Вопрос
I'm referring to the following example:
https://github.com/jaredhanson/passport-local/blob/master/examples/login/app.js
And specifically the following piece of code:
app.post('/login',
passport.authenticate('local', { failureRedirect: '/login', failureFlash: true }),
function(req, res) {
res.redirect('/');
});
The code as posted works just fine. However if I try to refactor it by doing the following:
app.post('/login',
authenticate,
function(req, res) {
res.redirect('/');
});
function authenticate() {
return passport.authenticate('local', { failureRedirect:'/fail', failureFlash:true });
}
things don't work anymore. What am I doing wrong?
Решение
I think you'll have to invoke authenticate()
(with the parens), in order to execute the function and return the middleware. For example:
app.post('/login',
authenticate(),
function(req, res) {
res.redirect('/');
});
The way you currently have it set up, authenticate is installed as a route handler, so express is calling it with req
, res
arguments (which aren't being used or responded to).
Не связан с StackOverflow