I have been wanting to stream results from Mongo through Hapi for a while and finally got around to it tonight. Since Mongo’s Node driver streams in object mode, I had to write a small implementation of a transform stream to support Hapi’s reply
interface.
The transform stream implementation simply stringifies each object as it comes in, separates these strings with commas, and wraps the response in square brackets to make it look like an array. Using a response stream means Hapi isn’t going to help us out with the Content-Type
header, so we have to set it ourselves.