Any alternative for req.files in NodeJS v0.12.5?


I need to upload a file CSV to later save it in the database, this with and , but due to project restrictions we are using NodeJS v0.12.5 and does not support req.files

Do you know any alternative or workaround to do this?

For reasons of confidentiality I can only put part of the code:

var Q = require('q');
var fs = require('fs');
var Joi = require('joi');
var parse = require('csv-parse');
var uploadModel = require('./upload.model');
var oemController = require('../oem/oem.controller');
exports.oemUpload = function(req, res) {
    var quote_number = req.params.quote_number;
    var files = req.files; // our node version is too old, it doesn't have req.files 

The error in console is: req.files is undefined. Testing on another computer with Node4 this does not happen.

asked by TonyMacross 02.02.2016 в 18:29

2 answers


You can use connect-busboy which allows you to upload files, supports multi-part and works from version v0.8.0 . You also have a permissive license.


var fs = require('fs');
var busboy = require('connect-busboy');

app.use(busboy());'/upload', function(req, res) {
    var fstream;
    req.busboy.on('file', function (fieldname, file, filename) {
        console.log("Subiendo: " + filename); 
        fstream = fs.createWriteStream(__dirname + '/files/' + filename);
        fstream.on('close', function () {
            res.send(200); // esto envia el 200 ok cuando sube todo el file
            // pero podrias usar un redirect o lo que necesites    
            // ej: req.redirect(req.header('Referer') || '/')   

By the way, on the page of expressjs / body-parser is one of the recommended options for uploading files. Along with connect-multiparty , formidable and multer

answered by 02.02.2016 в 18:40

Adding to the response from @rnrneverdies I prefer to use multer because it's easier and better suited to the philosophy of the middlewares of Express besides being very configurable makes your routes or drivers cleaner and only have to deal with the logic of your application. The module multer uses busboy internally so depending on your needs can greatly simplify the handling of files.

Here is an example:

var multer = require('multer');

var uploadConfig = multer({ dest: 'files/' });

var uploadConfig2 = multer({ dest: 'uploads/', limits: { fieldSize: 5000000} });'/upload', uploadConfig.single('oem'), function(req, res, next) {
    var file = req.file;
    // Mucho mas limpio
    // El valor de lo que va aquí depende de como hayas configurado el upload en este 
    // caso un solo fichero
});'/upload2', uploadConfig2.fields([{name: 'oem1', maxCount: 1}, {name: 'oem2', maxCount: 2}]), function() {
     var files = req.files;
     // Un arreglo con como máximo 3 ficheros y un máximo de tamaño de 5MB por cada uno

As in the example, you can use the methods of your variable uploadConfig.array and uploadConfig.fields to load several files at once, the last option being the most powerful in terms of configuration. I also gave an example of how you can have 2 completely different configurations in the same place, in the second you specify a different folder and a maximum size of 5Mb maximum for your files with 1Mb being the default configuration.

answered by 02.02.2016 в 22:18