Express Middleware两次调用Console.log

时间:2019-08-30 10:05:29

标签: node.js express logging

我正在使用快速中间件记录对节点服务器的所有请求。日志被调用两次,该如何避免呢?

question中的解决方案指出了可能的原因,但是我如何避免它或绕过fav.ico请求。

这是我的代码:

  app.use((req, res, next) => {
  console.log('Im being called', req.url);

  next();

 });

2 个答案:

答案 0 :(得分:0)

您可以排除带有包含您的网站图标名称或扩展名'.ico'的URL的请求:

app.use((req, res, next) => {
    if (!req.url.includes('.ico'))
        console.log('Im being called', req.url)
    next()
})

答案 1 :(得分:0)

这应该有效

app.use((req, res, next) => {
  if (req.url.includes('/favicon.ico')) {
    return next(); // specify return so the next console.log won't be executed
  }

  console.log("Im being called", req.url);  
});

希望有帮助