Node (express.js) next() is called before end of stream Node (express.js) next() is called before end of stream express express

Node (express.js) next() is called before end of stream


Use the writable file stream's close event to know when the file descriptor has been closed.

Replace this:

var writeStream = fs.createWriteStream(req.filePath);req.on('data', function(chunk) {    writeStream.write(chunk);});req.on('end', function() {    writeStream.end();    next();});

with this:

req.pipe(fs.createWriteStream(req.filePath)).on('close', next);