Search code examples
node.jsmiddlewarebody-parser

Bodyparser for multipart Nodejs


enter image description here enter image description hereSo I want to begin with my body-parsers and also I'm using the 'multer'

My multer options :

var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
    cb(null, '/root/Unicon-Oauth/Resources/profile_images/')
},
filename: function (req, file, cb) {
    cb(null, file.fieldname + '-' + Date.now())
}
});

var pfImage = multer({storage:storage});

Body Parsers at server.js

app.use(bodyParser.urlencoded({extended:true,limit: '20MB',parameterLimit:10000}));
app.use(bodyParser.json());

I have a route like that

router.post('/edit',[auth.isAuthenticated,pfImage.single('pImage')],actions.edit);

function is like that

function edit(req,res)
{
  console.log(req.body);
}

console log output :

Blockquote

{"------WebKitFormBoundaryGS8GEzQls8xRP6nt\r\nContent-Disposition: form-data; name":"\"_id\"\r\n\r\n58a4735cfa328b7e9eaf6a3a\r\n------WebKitFormBoundaryGS8GEzQls8xRP6nt\r\nContent-Disposition: form-data; name=\"city\"\r\n\r\nKayseri\r\n------WebKitFormBoundaryGS8GEzQls8xRP6nt\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\nali\r\n------WebKitFormBoundaryGS8GEzQls8xRP6nt\r\nContent-Disposition: form-data; name=\"\"\r\n\r\n\r\n------WebKitFormBoundaryGS8GEzQls8xRP6nt--\r\n"}

How can I parse this as req.body ?


Solution

  • The problem is that you're sending a multipart/form-data request but you're overriding the Content-Type and setting it to a different type (application/x-www-form-urlencoded), which is a totally different format. If you remove this override, it should be fine.