app.use和app.get(或app.post)node.js表达

时间:2012-11-19 08:44:22

标签: node.js express

我想使用app.use:

app.use(function(req, res, next){
    console.log('%s %s', req.method, req.url);
    next();
});

但它只有在我的代码中没有app.get或app.post时才有效,如果我有一个我的app.use是忽略。

我想在每次调用app.get或app.use时执行脚本

我也试试:

app.use(function(req, res, next){
    console.log('%s %s', req.method, req.url);
    app.get('/',function(req,res){console.log('aa')})
    next();
});

在这种情况下我的app.get是忽略

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您必须将app.use()放在路由器上方。路由器通常位于快速配置中,因此只需将app.use()置于配置之上即可。像这样:

app.use(function(req, res, next) {
  console.log('awesome');
  next();
})

app.configure(function(){
  // config
  app.use(app.router);
});

另请参阅logger中的构建。

相关问题