1

I created this small example to find out where the form data end up, but I can't figure out where. I would expect to find some data in req.body, but as you see if you run the example, if comes out empty :/

var connect = require('connect');
var bodyParser = require('body-parser');

var app = connect();

app.use(bodyParser.urlencoded({'extended': false}));

app.use(function(req, res){
    console.log(req);
    res.end("<html><form method='post'enctype='multipart/form-data'><input type='text' name='file_caption' /><input type='file' name='file_file' /><input type='submit' /></form></html>");
});

app.listen(3000);

The thing is, if I use connect-busboy to access the input and file I find them. But why aren't they visible in the request object?

Gustav
  • 1,361
  • 1
  • 12
  • 24

1 Answers1

0

body-parser doesn't provide any middleware to handle multipart requests. For that you need something like multer (multipart only), busboy/connect-busboy, or multiparty.

mscdex
  • 104,356
  • 15
  • 192
  • 153
  • I know, I have seen it working. But I wonder where is the data? I can't see it in the request, which is the only thing that connect-busboy (and all the others) has to work with, right? – Gustav Oct 01 '14 at 21:19
  • It's not possible to have seen a populated `req.body` if you've only been using the `body-parser` middleware and a `multipart/form-data` form submission. `body-parser` only recognizes `application/x-www-form-urlencoded` form submissions. – mscdex Oct 01 '14 at 21:24
  • I understand. But where is the data?!? The various middlewares parses some data right, which I think would be present in the request, but I can't find it. – Gustav Oct 01 '14 at 21:30
  • Look at the example, i log out the entire `req` object, which should be all the data coming from the request, but where's my data?! – Gustav Oct 01 '14 at 21:31
  • 1
    `req` is an [IncomingMessage](http://nodejs.org/api/http.html#http_http_incomingmessage) which is also a Readable stream, and that's where the raw data comes in from the client. The various body parsers read that data, parse it, and then some modules will set `req.body` (and possibly `req.files` if they support multipart) with the results of the parsing. – mscdex Oct 01 '14 at 21:37
  • Ah ok, so if I find the correct event on `req` to listen to, I would be able to read that data as well? You have any idea of what they would be? (heading to docs now...) – Gustav Oct 01 '14 at 21:40
  • 1
    The [Readable stream docs](http://nodejs.org/api/stream.html#stream_class_stream_readable) are what you're looking for. – mscdex Oct 01 '14 at 21:42