简体   繁体   English

Node中的req.body为空

[英]req.body is empty in Node

React (Client) sent post data through axios. React(客户端)通过axios发送帖子数据。 but req.body is empty in Node server side. 但是req.body在节点服务器端为空。 Tried to use body-parser. 尝试使用身体解析器。 but failed. 但失败了。 attached client side here 附加客户端在这里

attached server code here 附加服务器代码在这里

This is client Axios part 这是客户端Axios的一部分

It should be the Content-Type on the request. 它应该是请求的Content-Type。

Per default the body-parser "urlencoded" handles only the following: 默认情况下,主体解析器“ urlencoded”仅处理以下内容:

Content-Type: application/x-www-form-urlencoded;

You can set the type so: 您可以将类型设置为:

app.use(bodyParser.urlencoded({
  extended: true,
  type: 'multipart/form-data'
}))

But then you have to parse the "raw body" by yourself, because the body-parser doesn't support multipart. 但是随后您必须自己解析“原始主体”,因为主体解析器不支持多部分。

The body-parser doesn't support decoding multipart/form-data. 主体解析器不支持解码多部分/表单数据。 There are ample of libraries available for parsing multipart-form/data. 有很多库可用于解析多部分表单/数据。

I know the formidable library to be working and using it is as simple as this: 我知道强大的库可以正常工作并且使用它就像这样简单:

var form = new formidable.IncomingForm();

form.parse(req, function(err, fields, files) {

    console.log(`fields: ${fields} /n files: ${files}`)

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM