express.js:未定义的请求

发布于 2025-01-20 12:27:07 字数 651 浏览 1 评论 0原文

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

enter image description here

I tried to you a parser but seems like just wrapping void into an object

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睫毛上残留的泪 2025-01-27 12:27:07

作为我的评论的答案发布。

在邮递员的第二张图片中,您需要将类型下拉列表从文本更改为 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文