Does Express.js support sending unbuffered progressively flushed responses?

后端 未结 1 1802
遥遥无期
遥遥无期 2020-11-30 13:54

Perl\'s Catalyst framework permitts you to send an progressively flushed response over an open connection. You could for instance use write_fh() on Catalyst::Response. I\'ve

相关标签:
1条回答
  • 2020-11-30 14:32

    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.

    0 讨论(0)
提交回复
热议问题