【问题标题】:Get the res.send value in middleware for expressjs获取 expressjs 中间件中的 res.send 值
【发布时间】:2015-05-15 19:36:34
【问题描述】:

我想创建一些中间件来记录所有路由的请求正文和响应正文。记录请求是直截了当的,但是,我似乎无法得到响应。 res.on('end') 不会发出任何数据,我也没有看到任何与 express 相关的东西会发出或应用它。有人对如何实现这一点有任何建议吗?

编辑:根据 kook 回答中的链接,我想出了这个,这似乎运作良好。

app.use(function(req, res, next) {
    var send = res.send;
    res.send = function(body){
      console.log(body);
      res.send = send;
      res.send(body);
    };
    next();
});

【问题讨论】:

  • 似乎不想工作。我的设置为app.use(/myroute, myroute); app.use(seeResDotSend)。我在seeResDotSend 里面有一个console.log,作为第一行,它触发得很好。但它似乎永远不会到达res.send = function(body) { ... } 中的console.log。我做错了什么?

标签: node.js express


【解决方案1】:

你看过这些类似的帖子吗?我认为您应该能够按照他们的逻辑创建自己的:

express logging response body

Node.Js/ Express - simple middleware to output first few characters of response

【讨论】:

  • 是的,但是块值总是undefined
猜你喜欢
  • 1970-01-01
  • 2017-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-09
  • 2016-12-10
  • 1970-01-01
  • 2023-01-21
相关资源
最近更新 更多