Javascript 护照验证卡在控制器中?

Javascript 护照验证卡在控制器中?,javascript,node.js,typescript,passport.js,Javascript,Node.js,Typescript,Passport.js,我尝试将我的路线转发给管制员,但它似乎不适用于Passport.js router.get('/login', (req, res, next) => UserController.getLogin(req, res, next)); router.post('/login', (req, res, next) => UserController.postLogin(req, res, next)); 现在,唯一不起作用的路线就是那些有护照的路线 static getLogin(r

我尝试将我的路线转发给管制员,但它似乎不适用于Passport.js

router.get('/login', (req, res, next) => UserController.getLogin(req, res, next));
router.post('/login', (req, res, next) => UserController.postLogin(req, res, next));
现在,唯一不起作用的路线就是那些有护照的路线

static getLogin(req: Request, res: Response, next: NextFunction) {
...
}
static postLogin(req: Request, res: Response, next: NextFunction) {

        passport.authenticate('local', {
            successRedirect: '/success',
            failureRedirect: '/failed'
        });
        // res.send('hello from POST'); would work
}

我使用的是TypeScript,Passport是异步的。它通常用作传递回调的中间件。e、 例如,文档中有以下示例:

app.post('/login', passport.authenticate('local', { successRedirect: '/',
                                                failureRedirect: '/login' }));
这里要记住的是
passport.authenticate
返回一个接受
(req,res,next)
的函数。然后,它对该数据进行操作,并在完成后调用
next
。在代码中,您正在调用
身份验证
(返回一个函数),然后对其不做任何操作。我有一些建议

首先是通过简化事物来减少噪音。根据框架的不同,通常可以传递一组函数来处理路由。在这种情况下,您只需要一个

router.post('/login', passport.authenticate('local', {
    successRedirect: '/success',
    failureRedirect: '/failed'
}))
如果你想做的不仅仅是验证,你可以传递更多的函数

router.post('/login', 
    passport.authenticate('local', {
        successRedirect: '/success',
        failureRedirect: '/failed'
    }),
    UserController.doThing // accepts (req, res, next)
)

您会注意到,我没有创建匿名函数来将相同的3个参数传递给控制器。没必要。在大多数情况下,它们是相同的。

我试着模仿一下[link],并使用一个控制器
app.post(“/login”,userController.postLogin)这样,结构会更有条理。这不是正确的方法吗?你可以遵循这个模式。如果是这样,您希望为
authenticate
添加最终执行,并传入参数
req,res,next
<代码>。。。failureRedirect:'/failed'})(请求、恢复、下一步)
谢谢!实际上我忘记了自调用函数
passport.authenticate('local',{…})(req,res,next)工作正常