微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何拦截来自 Express 的状态和正文响应?

如何解决如何拦截来自 Express 的状态和正文响应?

我需要使用 Express 拦截响应的状态和正文。

我想用一个中间件来处理这个问题,并且看到了这些完美运行的代码行:

app.use((req,res,next) => {
    let oldSend = res.send
    res.send = function(data) {
        console.log(data) // do something with the data
        res.send = oldSend // set function back to avoid the 'double-send'
        return res.send(data) // just call as normal with data
    }
    next() })

我是 Node.js 的新手,并不太了解在这个中间件期间发生了什么。 我试图通过以下方式捕捉状态:

  app.use((req,next) => {
        let oldStatus = res.status
        res.status= function(data) {
            console.log(data) // do something with the data
            res.status= oldStatus // set function back to avoid the 'double-send'
            return res.status(data) // just call as normal with data
        }
        next() })

它也很好用,但现在我想获取状态和正文但不知道该怎么做...

提前谢谢

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。