Express 3.4.8 Photo uploading issue - how to solve without using bodyParser()? Express 3.4.8 Photo uploading issue - how to solve without using bodyParser()? mongoose mongoose

Express 3.4.8 Photo uploading issue - how to solve without using bodyParser()?


Have you given node-multiparty a try? Here's example usage from the README:

var multiparty = require('multiparty')  , http = require('http')  , util = require('util')http.createServer(function(req, res) {  if (req.url === '/upload' && req.method === 'POST') {    // parse a file upload    var form = new multiparty.Form();    form.parse(req, function(err, fields, files) {      res.writeHead(200, {'content-type': 'text/plain'});      res.write('received upload:\n\n');      res.end(util.inspect({fields: fields, files: files}));    });    return;  }  // show a file upload form  res.writeHead(200, {'content-type': 'text/html'});  res.end(    '<form action="/upload" enctype="multipart/form-data" method="post">'+    '<input type="text" name="title"><br>'+    '<input type="file" name="upload" multiple="multiple"><br>'+    '<input type="submit" value="Upload">'+    '</form>'  );}).listen(8080);

The author (Andrew Kelley) recommends avoiding bodyParser, so you're right to avoid it, but multiparty seems to solve a similar issue for me.


The reason it no longer works is because the node-formidable library is no longer included in Express. If you want to continue using formidable, follow these instructions.

The proper way to use body-parser with Express 4.x is:

var express = require('express'),    bodyParser = require('body-parser'),    app = express();app.use(bodyParser.json());app.use(bodyParser.urlencoded({ extended: false }));

To access the files:

var formidable = require('formidable'),    form = new formidable.IncomingForm();exports.submit = function (dir) {    return function (req, res, next) {        form.parse(req, function(err, fields, files) {            // The files object here is what you expected from req.files        });    });});

Note: if you're trying to use multiple, set:

form.multiples = true;