此问题已在此处有答案:
Why is req.body undefined?(2个答案)
10小时前关闭。
我正在尝试从网页向节点服务器发送一些json。
在客户端,我有:
const response = await fetch("http://localhost:3000/bb", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ message: "Hello, server!" }),
});
console.log(JSON.stringify({ message: "Hello"}));
我可以看到{ message:“Hello”}。
我在我的node应用程序中有以下代码:
// POST route
app.post('/bb', async (req, res) => {
try {
// Assuming you're receiving JSON data in the request body
const newData = await req;
// Process the new data here
console.log('Received new data:', newData);
res.status(200).json({ message: 'Data received successfully' });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.listen(3000, () => console.log('Server started on port 3000'));
在服务器端我看到
Received new data: undefined
我做错了什么?
1条答案
按热度按时间qjp7pelc1#
看这里像我一样做