mscdex / connect-busboy

Connect middleware for busboy
MIT License
155 stars 25 forks source link

busboy 0.2.6 compatible? #3

Closed hacksparrow closed 10 years ago

hacksparrow commented 10 years ago

Is connect-busboy compatible with busboy 0.2.6? Been hacking for almost an hour, can't get it working.

Maybe update the examples to make the usage more obvious?

mscdex commented 10 years ago

Yes it is compatible. This module is basically just a thin wrapper to attach a busboy instance to the req object. What are you having trouble with in particular?

hacksparrow commented 10 years ago

The post handler keeps hanging there; even the busboy events handlers are not executed. The "here!" message is printed, though.

var busboy = require('connect-busboy');
// load the middleware
app.use(busboy({ immediate: true }));
// define a POST route
app.post('/', function(req, res) {
  if (req.method === 'POST') {
    console.log('here!');
    req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
      console.log(filename);
    });
    req.busboy.on('field', function(key, value, keyTruncated, valueTruncated) {
      console.log(key, value);
    });
  }
});

Am I doing something wrong?

mscdex commented 10 years ago

You're not sending a response to the request. Using immediate: true can be tricky because you could miss events if connect-busboy and the route handler are not called within the same tick.

hacksparrow commented 10 years ago

Well, I am not concerned about sending the response at the moment, I want to get the busboy events firing first. I am not getting the console.log messages.

I have removed immediate: true, still not helping.

mscdex commented 10 years ago

If you remove immediate: true, then you have to pipe the request yourself: req.pipe(req.busboy);

hacksparrow commented 10 years ago

Updated code. No busboy events fired.

var busboy = require('connect-busboy');
// load the middleware
app.use(busboy());
// define a POST route
app.post('/', function(req, res) {

  req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
    console.log(filename);
  });
  req.busboy.on('field', function(key, value, keyTruncated, valueTruncated) {
    console.log(key, value);
  });
  req.busboy.on('finish', function() {
    console.log('OVER!');
    res.send('OK!');
  });
  req.pipe(req.busboy);

});
mscdex commented 10 years ago

It works for me:

var busboy = require('connect-busboy'),
    app = require('express')();
app.get('/', function(req, res) {
  res.send('<html><head></head><body>\
               <form method="POST" enctype="multipart/form-data">\
                <input type="text" name="textfield"><br />\
                <input type="file" name="filefield"><br />\
                <input type="submit">\
              </form>\
            </body></html>');
});
// load the middleware
app.use(busboy());
// define a POST route
app.post('/', function(req, res) {

  req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
    console.log('file', filename);
    file.resume();
  });
  req.busboy.on('field', function(key, value, keyTruncated, valueTruncated) {
    console.log('field', key, value);
  });
  req.busboy.on('finish', function() {
    console.log('OVER!');
    res.send('OK!');
  });
  req.pipe(req.busboy);

});
app.listen(8000);
hacksparrow commented 10 years ago

I got it. It happens if enctype is not multipart/form-data.

Try without the enctype attribute or with its value set to application/x-www-form-urlencoded.

mscdex commented 10 years ago

It still works for me even when I remove the enctype or specify application/x-www-form-urlencoded explicitly.

hacksparrow commented 10 years ago

Express version? I am using 4.2.0.

hacksparrow commented 10 years ago

Upgraded to 4.3.2, still the same.

hacksparrow commented 10 years ago

Sorry for not including all the details. The culprit was bodyParser.urlencoded().

Are Busboy and bodyParser mutually exclusive?

mscdex commented 10 years ago

busboy can do both application/x-www-form-urlencoded and multipart/form-data.

body-parser only supports application/x-www-form-urlencoded and application/json.