Does Express.js support sending unbuffered progressively flushed responses?

佐手、 提交于 2019-11-27 05:25:00

Express is built on the native HTTP module, which means res is an instance of http.ServerResponse, which inherits from the writable stream interface. That said, you can do this:

app.get('/', function(req, res) {
  var stream = fs.createReadStream('./file.csv');
  stream.pipe(res);

  // or use event handlers
  stream.on('data', function(data) {
    res.write(data);
  });

  stream.on('end', function() {
    res.end();
  });
});

The reason you can't use the res.send() method in Express for streams is because it will use res.close() automatically for you.

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