[英]Node JS POST request returning undefined body
Quite new to Node JS and I am trying to post to a specific URL and retrieve the data. Node JS 的新手,我正在尝试发布到特定的 URL 并检索数据。
I am using postman to do this but every time I post to it the response data is undefined but the status code is 200.我正在使用邮递员来执行此操作,但是每次我向其发布响应数据时,响应数据都未定义,但状态代码为 200。
I have added body-parse as suggested but still no joy.我已经按照建议添加了 body-parse,但仍然没有任何乐趣。
I will post my server code and request below.我将在下面发布我的服务器代码和请求。
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = process.env.PORT || 5000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/api/hello', (req, res) => {
res.send({ express: 'Hello From Express' });
});
app.post('/api/save-json', (req, res) => {
console.log(req);
res.send(
`I received your POST request. This is what you sent me: ${req.body.json}`,
);
});
app.listen(port, () => console.log(`Listening on port ${port}`));
My postman request is:我的邮递员要求是:
{
"body" :
{
"json": "some data"
}
}
Any ideas?有任何想法吗?
body
是包含 HTTP 请求正文(由body-parser
设置)的req
对象中的属性,因为您发送的是一个名为body
的对象,您需要像这样访问它: req.body.body.json
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.