0

Before you wast any time : please note that this is a reference question , just in case some one needed it. but please feel free to correct me :)

so , im trying to use formidable (i know that it's so similar to bodyParser and that i should be using body parser instead ! ) with express . the problem that it doesnt work at all .

here is simple part of my code (relevant part )

    form.on('progress', function (bytesReceived, bytesExpected) {
        console.log(bytesExpected);
        console.log('progress ');
        if (bytesReceived > options.maxPostSize) {
            console.log('bla ');
            req.connection.destroy();
        }
    }).on('end', finish).parse(req,function(err, fields, files) {
            console.log(files);
});

now if i try to console.log -> err , fields , or files it doesnt work .

an the only event that is being emitted is progress .

2
  • Note: It's not just similar to bodyParser(). Connect/Express uses formidable for multipart requests. Commented Aug 29, 2013 at 19:09
  • @JonathanLonowski well thanks ..! this only makes me happy to post what i face and solve here .. thanks again . Commented Aug 29, 2013 at 19:15

2 Answers 2

2

if you use app.use(express.bodyParser()) is equivalent to:

app.use(express.json());
app.use(express.urlencoded());
app.use(express.multipart());

the multipart use formidable, so events doesn't trigger,you can remove app.use(express.multipart()); then you can use formidable object.

0
0

The problem is caused by the parse function . To solve this you need to not use the bodyParser . So let's assume that i have a route /files which only recives a post request then you need to disable the bodyParser just for that route like so :

app.use(function(req,res,next){

  // important .. this will skip body parser to avoide issues with formidable 

    if((req.path == '/file' ||req.path == '/file/') &&req.method === 'POST'){
   // GET , PUT , DELETE ,etc... will have bodyParser ^^^^^^^^^^^^^^^^^^^^^
        return next();
      }
    express.bodyParser(req,res,next);
    next();
});

Not the answer you're looking for? Browse other questions tagged or ask your own question.