如何编写一个仅捕获操作的快速路由中间件?

问题描述 投票:0回答:2

我正在使用以下中间件:

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

但它捕获每个请求,包括图像/css/js 等...

如果我只是想捕捉动作,我该怎么办?

node.js express node.js-connect
2个回答
3
投票

您可以将“静态处理程序”中间件放在此中间件之前,如果请求与静态中间件路由匹配,它根本不应该到达您的中间件。

所以尝试一下:

app.use(express.static(__dirname + '/public'));
app.use(function(req,res,next){
  console.log(req.url)
  next()
});

0
投票

关于express和connect中间件的详细解释查看这个url http://www.hacksparrow.com/how-to-write-middleware-for-connect-express-js.html

© www.soinside.com 2019 - 2024. All rights reserved.