nodejs npm body parser返回空体

时间:2018-02-02 21:43:05

标签: node.js body-parser

我正在使用身体解析器示例

var express = require('express')
var bodyParser = require('body-parser')

var app = express()

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

app.use(function (req, res) {

  console.log(req)

  res.setHeader('Content-Type', 'text/plain')
  res.write('you posted:\n')
  res.end(JSON.stringify(req.body, null, 2))
})

enter image description here

但是当我使用邮递员发布数据时,req.body返回空值。

但正如您在屏幕截图中看到的那样,我有内容{message:12345}

在帖子中。

代码运行没有错误。

但我收到了回复

you posted:
{}

我应该得到的是

you posted:
{message:12345}

我回应了req对象,我可以看到身体是空的。

req: [Circular],
 locals: {},
 [Symbol(outHeadersKey)]: { 'x-powered-by': [Array] } },
body: {} }

有任何建议吗?

1 个答案:

答案 0 :(得分:1)

您需要发送s的内容类型标头。点击'文字'邮递员下拉选择JSON。

相关问题