Reputation: 2557
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 .
Upvotes: 0
Views: 1559
Reputation: 455
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.
Upvotes: 2
Reputation: 2557
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();
});
Upvotes: 0