I am currently using multer for multipart/form-data
in node.js application alongside with body-parser.
I tried to POST form-data using POSTMAN, but it is getting this error.
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
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)
.
Note: body-parser
version is 1.12.0 and multer
version is 0.1.8.
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.