【问题标题】:Why res. body is undefined in express为什么要水。身体在快递中未定义
【发布时间】:2021-06-05 11:27:51
【问题描述】:

为什么要保留。身体在快递中未定义 我怎样才能得到响应体, 有什么问题吗?

import {createProxyMiddleware} from 'http-proxy-middleware'
import bodyParser from 'body-parser'
import mung from "express-mung";

const app = express();


app.use(((req, res, next) => {
    console.log(res.body)

    next()
}))

app.get('/as', function (req, res, next) {

    res.send("123")
})
app.listen(3001, () => console.log('start in http://localhost:3001'))

【问题讨论】:

  • 你能详细解释一下你的问题吗?
  • 我想查看中间件中res.send的内容并修改,结果res.body总是未定义
  • app.use(function (req, res, next) { console.log('Time:', Date.now()) next() }) 每次应用收到请求时都会执行此代码。当您通过 get 收到请求时,您可以看到 console.log(req) 但在快递中它是从服务器端使用 res 对象发送数据,所以很自然地得到 res.body undefined.expressjs.com/en/api.html#res。此外,如果它是包含一些数据的发布请求,那么您也将使用 req.body 而不是 res.body。 req 对象是你得到的, res 是你从服务器发送的。
  • @edcjian 这里的意图是在将主体发送回客户端之前对其进行变异吗?即你想让res.bodyres.send() 被调用后给你"123"

标签: javascript node.js express


【解决方案1】:

req = 请求,res = 响应

您需要记录 req.body

app.use(((req, res, next) => {
    console.log(req.body)
    next()
}))

【讨论】:

    【解决方案2】:

    如果对你有帮助,请检查我的逻辑,

    app.post('/as', function (req, res, next) {
        console.log(req.body)
    })

    为了在控制台中为 req.body 打印一些东西,你需要传递一个“body” 从你称之为的地方。例如参考截图enter image description here

    【讨论】:

      【解决方案3】:

      这个答案是我的预期并解决了我的问题

      express.js - how to intercept response.send() / response.json()

      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()
      })
      

      【讨论】:

        【解决方案4】:

        您似乎还没有解析正文数据。

        req.body

        包含在请求正文中提交的数据键值对。默认情况下,它是未定义的,当您使用 express.json()express.urlencoded() 等正文解析中间件时会填充它。

        Express 自带两个body parser 方法,试试这样使用

        app.use(express.urlencoded());
        app.use(express.json());
        

        【讨论】:

          【解决方案5】:

          Express不像koa,response没有'body'属性,但是你可以创建一个自定义的中间件来解决这个问题,有一个例子:

          const logResponseBodyMiddleware = (req, res, next) => {
            const selfWrite = res.write
            const selfEnd = res.end
            const chunks = []
          
            res.write = (...args) => {
              const chunk = args[0]
              chunks.push(chunk)
              return selfWrite.apply(res, args)
            }
            res.end = (...args) => {
              const chunk = args[0]
              if (chunk) chunks.push(chunk)
              res.body = Buffer.concat(chunks).toString('utf8')
              selfEnd.apply(res, args)
            }
          
            next()
          }
          
          app.use(logResponseBodyMiddleware)
          
          • Node.js:12.21.0
          • 快递:4.17.1

          参考

          http_response_write
          http_response_end

          【讨论】:

            猜你喜欢
            • 2019-03-17
            • 2022-08-17
            • 1970-01-01
            • 1970-01-01
            • 2018-10-17
            • 1970-01-01
            • 2014-09-15
            • 1970-01-01
            • 2016-02-09
            相关资源
            最近更新 更多