express.js:未定义的请求
const express = require('express');
const cors = require('cors');
const app = express();
app.use(express.json())
app.use(cors());
app.post('/', (req,res) => {
console.log(req.body);
res.send('received')
})
app.listen(5000, console.log('Server is running on port 5000'));
我试图将数据发送到后端,但总是未定义
我尝试给你一个解析器,但似乎只是将 void 包装到对象中
const express = require('express');
const cors = require('cors');
const app = express();
app.use(express.json())
app.use(cors());
app.post('/', (req,res) => {
console.log(req.body);
res.send('received')
})
app.listen(5000, console.log('Server is running on port 5000'));
I am trying to send data to the back-end but always getting undefined
I tried to you a parser but seems like just wrapping void into an object
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为我的评论的答案发布。
在邮递员的第二张图片中,您需要将类型下拉列表从文本更改为 JSON,express.json() 仅在标头内容类型设置为“application/JSON”时解析正文,并且更改该下拉列表将自动包含该标头。
这里是 docs 如果它有助于解释为什么你在第二张图片
Posting as answer from my comment.
In the second image in postman you need to change the type dropdown from text to JSON, express.json() only parses the body when the header Content-type is set to "application/JSON" and changing that dropdown will automatically include that header.
Here are the docs if it helps explain why you get the empty object in the second image