使用node/express在req.body中的html multipart/form-data错误 [英] html multipart/form-data error in req.body using node/express

查看:101
本文介绍了使用node/express在req.body中的html multipart/form-data错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用node,express,html,并且我正在尝试使用html表单将a发布到我的服务器端.问题是我得到{}作为我的要求.

I am using node, express, html and i am trying to post a to my server side using a html form. The problem is I get {} as my req.body.

我的html表单如下:

My html form is the following:

    <form method = 'post' action='get_name' enctype="multipart/form-data">
      <input type="text" name="form_name"><br>
      <input type="submit" value="Upload name">
    </form>

在我的node.js文件的开头使用以下内容:

I use the following in the begining of the my node.js file:

app.use(bodyParser.urlencoded({limit:'5mb', extended:false}));
app.use(busboy());

我的app.post如下:

My app.post is the following:

app.post('/get_name',function(req, res, next){
        console.log("the name of the form is : ", req.body);
        res.redirect('/admin');
});

当我尝试获取req.body.form_name时,我不确定.我找不到我的代码有什么问题.欢迎任何建议.:)

When i am trying to get req.body.form_name I get undefined. I cant find out what is wrong with my code. Any suggestions are welcome. :)

推荐答案

如果要使用busboy,请遵循以下文档:

If you're going to use busboy, you should follow the documentation:

https://github.com/mscdex/busboy

否则,bodyParser()不支持多部分表单数据.我个人推荐此库是因为它很简单:

Otherwise, bodyParser() does not support multi-part form data. I personally recommend this library for it's simplicity:

https://www.npmjs.com/package/multer

这将以您打算使用的方式填充请求正文.

This will populate req.body the way you are intending to use it.

这篇关于使用node/express在req.body中的html multipart/form-data错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆