nodejs对所有请求执行公共操作

Kum*_*mar 6 node.js express

我正在使用带有express的节点js.现在我需要对所有请求执行常见操作.饼干检查

app.get('/',function(req, res){
   //cookie checking
   //other functionality for this request 
}); 

app.get('/show',function(req, res){
   //cookie checking
   //other functionality for this request 
}); 
Run Code Online (Sandbox Code Playgroud)

这里cookie检查是所有请求的常见操作.那么如何在不重复所有app.get中的cookie检查代码的情况下执行此操作.

解决此问题的建议?提前致谢

Pet*_*ons 8

查看路由中间件上的快速文档中loadUser示例.模式是:

function cookieChecking(req, res, next) {
    //cookie checking
    next();
}


app.get('/*', cookieChecking);

app.get('/',function(req, res){
    //other functionality for this request 
}); 

app.get('/show',function(req, res){
   //other functionality for this request 
}); 
Run Code Online (Sandbox Code Playgroud)


bal*_*ton 3

app.all或使用中间件。