Skip to content Skip to sidebar Skip to footer

Node (express.js) Next() Is Called Before End Of Stream

I have the following middleware function var bodyParser = require('body-parser'), fs = require('fs'); module.exports = function(req, res, next) { // Add paths to this array to

Solution 1:

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);

Post a Comment for "Node (express.js) Next() Is Called Before End Of Stream"