0

I am trying to use busboy middleware to get the body from a POST request.

I have the following in the main server.js

//file upload middleware
var busboy = require('connect-busboy');
app.use(busboy());

I have a route set up like so:

app.post('/create', function(req, res){

    req.pipe(req.busboy);
    req.busboy.on('finish', function (fieldname, file, filename) {
        console.log(req.body);
    });

});

But if I send a request to the endpoint i get the following error:

_stream_readable.js:501
    dest.end();
         ^
TypeError: Cannot call method 'end' of undefined
    at IncomingMessage.onend (_stream_readable.js:501:10)
    at IncomingMessage.g (events.js:180:16)
    at IncomingMessage.emit (events.js:92:17)
    at _stream_readable.js:943:16
    at process._tickCallback (node.js:419:13)

How can I read the body inside of this route?

wazzaday
  • 9,474
  • 6
  • 39
  • 66

1 Answers1

2

Your finish event handler is wrong. It looks like a typo and should be a file event handler instead.

Secondly, it looks like the request's Content-Type is not correct. It should either be multipart/form-data either application/x-www-form-urlencoded.

Lastly, req.body will not be set inside your event handler because you're not populating it, so that will always be undefined.

mscdex
  • 104,356
  • 15
  • 192
  • 153