json-array-stream

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')
 
  db.things.find()
  .stream()
  .pipe(streamify())
  .pipe(res)
})

will yield something like

[
{"_id":"123412341234123412341234"}
,
{"_id":"123412341234123412341234"}
]
  • The stream always starts with '[\n'.
  • Documents are separated by '\n,\n'.
  • The stream is terminated with '\n]'.

Returns a duplex stream.

WTFPL