如何在express中访问post请求的正文?

问题描述 投票:0回答:1

我一直在尝试向节点发送 post 请求,但我通过 post 请求正文传递的数据未显示在 request.body 中。如何访问 post 请求正文?

这是我用来接收帖子请求的代码

   const express = require('express');

    const request = require('request');

    const app = express();

    app.use(express.json());

    app.post('/', async (req, res) => {
    try {
    const locationName = req.body;
    console.log(req.body);
    res.status(200).json({requestBody: req.body});
    } catch (err) {
    console.error(err);
    res.status(500).send('Internal Server Error');
    }
    });

    const PORT = process.env.PORT || 3000;
    app.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
    });

当我尝试向邮递员发出发帖请求时,它显示为

{
     {}
}

node.js express post request
1个回答
0
投票

代码没问题。 当您从邮递员发出发布请求时,您需要在请求正文中选择行数据 JSON 格式

© www.soinside.com 2019 - 2024. All rights reserved.