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

主宰稳场 提交于 2019-12-11 00:24:57

问题


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 allow binary uploads
  var pathsAllowingBinaryBody = [
    '/api2/information/upload',
    '/api2/kpi/upload',
  ];

  if (pathsAllowingBinaryBody.indexOf(req._parsedUrl.pathname) !== -1) {
    var date = new Date();
    req.filePath = "uploads/" + date.getTime() + "_" + date.getMilliseconds() + "_" + Math.floor(Math.random() * 1000000000) + "_" + parseInt(req.headers['content-length']);

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

The files is being transfered correctly however sadly the next() in the

req.on('end', function() {
  writeStream.end();
  next();
});

is called before it is done writing all data to the new file.

My question is what am i doing wrong? And how can i fix it?


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


来源:https://stackoverflow.com/questions/36312637/node-express-js-next-is-called-before-end-of-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!