File uploading with Express 4.0: req.files undefined

express, node.js

Solution

The `body-parser` module only handles JSON and urlencoded form submissions, not multipart (which would be the case if you're uploading files).

For multipart, you'd need to use something like `connect-busboy` or `multer` or `connect-multiparty` (multiparty/formidable is what was originally used in the express bodyParser middleware). Also FWIW, I'm working on an even higher level layer on top of busboy called `reformed`. It comes with an Express middleware and can also be used separately.

Problem

I'm attempting to get a simple file upload mechanism working with Express 4.0 but I keep getting `undefined` for `req.files` in the `app.post` body. Here is the relevant code: ``` var bodyParser = require('body-parser'); var methodOverride = require('method-override'); //... app.use(bodyParser({ uploadDir: path.join(__dirname, 'files'), keepExtensions: true })); app.use(methodOverride()); //... app.post('/fileupload', function (req, res) { console.log(req.files); res.send('ok'); }); ``` .. and the accompanying Pug code: ``` form(name="uploader", action="/fileupload", method="post", enctype="multipart/form-data") input(type="file", name="file", id="file") input(type="submit", value="Upload") ``` Solution Thanks to the response by mscdex below, I've switched to using `busboy` instead of `bodyParser`: ``` var fs = require('fs'); var busboy = require('connect-busboy'); //... app.use(busboy()); //... app.post('/fileupload', function(req, res) { var fstream; req.pipe(req.busboy); req.busboy.on('file', function (fieldname, file, filename) { console.log("Uploading: " + filename); fstream = fs.createWriteStream(__dirname + '/files/' + filename); file.pipe(fstream); fstream.on('close', function () { res.redirect('back'); }); }); }); ```

Original source