0

I am working with expressjs 4.12.3, and trying to connect to connect-busboy, but on request I am not able to get req.busboy object, it says "undefined" my simple code is as follows :

var express=require('express');
var busboy = require('connect-busboy');
var app=express();
app.use(busboy());

app.use(function(req, res, next) {
   req.busboy.on('field', function(fieldname, val) {
     // console.log(fieldname, val);
     req.body[fieldname] = val;
   });

   req.busboy.on('finish', function(){
     next();
   });
 });
 
 app.listen(5555);

I have initialize busboy module, assigned it to the app, also sending content-length: "5276" content-type:'application/x-www-formurlencoded' as headers.

what am I doing wrong??

vijay kumar
  • 65
  • 1
  • 7
  • [The request method is also not GET or HEAD](https://github.com/mscdex/connect-busboy#typeerror-cannot-call-method-on-of-undefined)? – aarosil Mar 24 '15 at 06:36
  • request method is 'POST' – vijay kumar Mar 24 '15 at 11:21
  • I used [`multer`](https://www.npmjs.com/package/multer), worked seamlessly, without listening to any events – aarosil Mar 24 '15 at 15:26
  • I will give it a try with multer, even this code should work, to be sure I just checked the req.method in node-inspector, it says "POST" now I am totally clueless why it is behaving so. – vijay kumar Mar 24 '15 at 16:47
  • Used multer library which works like a charm, but I still would like to know what is wrong with above code, any suggestions? – vijay kumar Mar 24 '15 at 21:38
  • Dunno, probably you'd need look into the source code. :-) – aarosil Mar 24 '15 at 21:50

1 Answers1

0

The problem is that you're setting up event handlers, but you're not actually piping the request to busboy so it can parse the request. Add req.pipe(req.busboy); after your busboy event handlers and it should work fine.

EDIT: I slightly misread your question. If req.busboy is undefined that means the Content-Type is wrong. If your Content-Type really is application/x-www-formurlencoded, then that is wrong. It should be: application/x-www-form-urlencoded.

mscdex
  • 104,356
  • 15
  • 192
  • 153
  • Hi thanks for the reply, I tried with "application/x-www-form-urlencoded" issue still persist, with respect to events, I thought "app.use(busboy())" takes care of that? – vijay kumar Mar 31 '15 at 14:49