I am currently using multer for multipart/form-data
in node.js application alongside with body-parser.
我目前正在使用multer来处理多部分/表单数据。与body-parser结合的js应用程序。
I tried to POST form-data using POSTMAN, but it is getting this error.
我尝试使用POSTMAN发布表单数据,但是它会得到这个错误。
Error: invalid json
at parse (/Users/k/Documents/application/node_modules/body-parser/lib/types/json.js:79:15)
at /Users/k/Documents/application/node_modules/body-parser/lib/read.js:102:18
at IncomingMessage.onEnd (/Users/k/Documents/application/node_modules/body-parser/node_modules/raw-body/index.js:136:7)
at IncomingMessage.g (events.js:199:16)
at IncomingMessage.emit (events.js:104:17)
at _stream_readable.js:908:16
at process._tickDomainCallback (node.js:381:11)
This is how I added multer and body-parser in my server.js
这就是我如何在我的server.js中添加multer和body-parser
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var multer = require('multer');
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(multer({ dest: './uploads/'}));
I think it is probably because multer is not working. When I remove body-parser and try again, it shows empty json for console.log(req.body)
and console.log(req.files)
.
我想这可能是因为莫特没有工作。当我删除body解析器并再次尝试时,它显示了console.log(req.body)和console.log(req.files)的空json。
Note: body-parser
version is 1.12.0 and multer
version is 0.1.8.
注意:body-parser版本是1.12.0,multer版本是0.1.8。
1 个解决方案
#1
16
The problem is not that you have both libraries body-parser
and multer
. If you try to post with a browser extension like postman, please make sure Content-Type
is multipart/form-data
and not application/json
in your request header.
问题不在于您同时拥有库体解析器和multer。如果您尝试使用postman之类的浏览器扩展来发布,请确保内容类型是多部分/表单数据,而不是请求头中的应用程序/json。
#1
16
The problem is not that you have both libraries body-parser
and multer
. If you try to post with a browser extension like postman, please make sure Content-Type
is multipart/form-data
and not application/json
in your request header.
问题不在于您同时拥有库体解析器和multer。如果您尝试使用postman之类的浏览器扩展来发布,请确保内容类型是多部分/表单数据,而不是请求头中的应用程序/json。