我不想在每个
app.get()
的顶部放置验证功能,我该如何在每个请求之前执行代码,而不是在 app.get()
之前?在路由之前设置一个中间件:
function myMiddleware (req, res, next) {
if (req.method === 'GET') {
// Do some code
}
// keep executing the router middleware
next()
}
app.use(myMiddleware)
// ... Then you load the routes
您还可以进行以下操作:
app.all('*', auth.requireUser);
auth.requireUser
的路由吗? - Johhan Santana
next()
,我得去查一下。 - Farzher