Early flush with Node.js and Express

Problem

How do I realise early flush (chuncked transfer encoding) with Express?

All examples I have found are dealing with the http module, where you can call the write() method of the response object and that way send data piece-wise.

Problem courtesy of: user3033490

Solution

You can still use write with Express:

app.get('/test', function(req, res) {
  var count     = 0;
  var interval  = setInterval(function() {
    if (count++ === 5) {
      clearInterval(interval);
      res.end();
    }
    res.write('This is line #' + count + '\n');
  }, 1000);
});
Solution courtesy of: robertklep

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.