node.jsexpressbusboy

busboy not firing field, file event


In below code, only finish event called.


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

app.use(Busboy());

app.post('/fileupload', function(req, res) {
  var fstream;

  req.pipe(req.busboy);

  req.busboy.on('error', function(err){
    console.log(err);
  });

  req.busboy.on('field', function(fieldname, val, valTruncated, keyTruncated) {
    console.log("fieldname: " + fieldname); 
  });

  req.busboy.on('file', function (fieldname, file, filename) {
    console.log("filename: " + filename); 
    fstream = fs.createWriteStream(__dirname + '/files/' + filename);
    file.pipe(fstream);
    fstream.on('close', function () {
      res.redirect('back');
      console.log("fileupload end");
    });
  });

  req.busboy.on('finish', function() {
    console.log('Done parsing form!');
  });
});

Solution

  • The reason why you're not seeing any data is because you're already using the multer module which also parses multipart/form-data requests, saving files to disk. If you're not using multer and want to use busboy manually as you show in your code, you will need to remove the app.use(multer()); line.