multer not working outside of the http object

Created on 23 Apr 2019  路  4Comments  路  Source: expressjs/multer

Hi,
I am using multer and it works fine in the http object, here is the code:

server = http.createServer(function(req, res){
  var upload = multer({ storage : multerhelper.storage}).single('userFile');
  upload(req, res, function(err) {
    if(err)
      console.log("Error uploading the file");
  });
});

The moment I take this piece of code outside of the http object inside another file, it doesn't work anymore.

handlers._users.post = function(req, res, data, callback){
    var upload = multer({ storage : multerhelper.storage}).single('userFile');
    upload(req, res, function(err) {
      if(err)
        console.log("Error uploading the file");
      callback(400, {'Message' : 'Done'});
    });
});

Your help is appreciated.

All 4 comments

Could it be that the body is already consumed by another middleware? Without seeing any more of the code it's impossible for me to tell...

I would recommend posting general questions to Stack Overflow or another forum, where you are more likely to get help 鈽猴笍

Hi Linus,

It would make sense if the body of the request gets consumed, I was not aware of the possibility of such a thing. I thought passing the request as a parameter should keep it whole.

I will dig in that direction and ask on Stack Overflow if I have any further questions,
Thank you.

Yeah, the request body is a ReadableStream as an abstraction on top of the underlying socket, so you can only read from it once. This is done deliberately as to not keep the entire response in memory at any one time.

Good luck!

Interesting, thanks a lot for your generous contribution.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ChristianRich picture ChristianRich  路  4Comments

josephstgh picture josephstgh  路  3Comments

donjae picture donjae  路  4Comments

Gabxi picture Gabxi  路  3Comments

tonghae picture tonghae  路  4Comments