连接或 Express 中间件来修改 response.body [英] Connect or Express middleware to modify the response.body

查看:35
本文介绍了连接或 Express 中间件来修改 response.body的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个修改响应正文的中间件函数.

I would like to have a middleware function which modifies the response body.

这是一个快速服务器.

类似于:

function modify(req, res, next){
  res.on('send', function(){
    res.body = res.body + "modified"
  });

  next();
}

express.use(modify);

我不明白要监听什么事件.任何帮助或文档将不胜感激.

I don't understand what event to listen for. Any help or documentation would be appreciate.

推荐答案

您不需要监听任何事件.做吧

You don't need to listen to any events. Just make it

function modify(req, res, next){
  res.body = res.body + "modified";

  next();
}

并且在您use路由器之后use它.这样在执行完所有路线后,您就可以修改正文

And use it after you use the router. This way after all your routes have executed you can modify the body

这篇关于连接或 Express 中间件来修改 response.body的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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