app.use和app.get(或app.post)node.js表达 [英] app.use and app.get (or app.post) node.js express

查看:61
本文介绍了app.use和app.get(或app.post)node.js表达的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用一个应用程序.使用:

I'd like to use a app.use:

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

但是仅当我的代码中没有app.get或app.post时,如果我的app.use被忽略,它才有效.

But it only works if I have no app.get or app.post in my code, if I have one my app.use is ignore.

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

I'd like to execute a script each time a app.get or a app.use is called

我也尝试:

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被忽略

in this case my app.get is ignore

感谢您的帮助

推荐答案

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

you have to put your app.use() above the router. The router is usually inside the express configuration so just put your app.use() above the configuration. Something like this:

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

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

还可以在记录器中查看构建.

这篇关于app.use和app.get(或app.post)node.js表达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆