Streaming JSON.stringify() that returns an array

JSON Array Stream

The main use case for this is to stream a MongoDB query to a web client. This is to be used only with streaming arrays, not objects.

var streamify = require('json-array-stream')
app.get('/things', function (reqresnext) {
  res.setHeader('Content-Type', 'application/json; charset=utf-8')

will yield something like

  • The stream always starts with '[\n'.
  • Documents are separated by '\n,\n'.
  • The stream is terminated with '\n]'.

Returns a duplex stream.