【问题标题】:How to get body form data in nodejs express?如何在 nodejs express 中获取正文表单数据?
【发布时间】:2021-10-18 17:00:02
【问题描述】:

app.post('/hide_feed', middleware.authenticateToken, (req, res, next) => {
    if (req.body.followered_to_id) {
        return res.status(400).json({
            status: 400,
            msg: req.body.followered_to_id
        });
    }
});

尝试添加

app.use(express.urlencoded({
    extended: false // also true
}));

仍然无法获取body表单数据

【问题讨论】:

  • 发送 JSON 数据或普通文本的 urlencoded 和图像或文件的 formData

标签: node.js express postman


【解决方案1】:

您可以使用multer npm 包为您解析多部分表单数据。这是一个简单的中间件,所以应该很容易使用。更多关于multer

urlencoded 中间件(您使用的)处理 x-www-form-urlencoded 内容类型。

【讨论】:

  • 谢谢!!试过 multer 并且它工作得很好
【解决方案2】:

尝试使用 multer

app.post('/hide_feed', middleware.authenticateToken, (req, res, next) => {
    let upload = multer({
        storage: storage,
    }).single('');
    upload(req, res, function(err) {
        if (err) {
            return res.status(400).json({
                status: 400,
                msg: "fail"
            });
        } else {
            if (req.body.followered_to_id) {
                return res.status(400).json({
                    status: 400,
                    msg: req.body.followered_to_id
                });
            } else {
                return res.status(400).json({
                    status: 400,
                    msg: "fail"
                });
            }
        }
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 2012-03-07
    • 1970-01-01
    • 2023-03-11
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多