Introduction
To quote Dominic Tarr, the Streams API is Node's "best and most misunderstood idea". Throughout this book, recipes often touch on the Streams API. Streams are fundamental to the Node platform and are utilized in many of the core modules.
A stream is basically an object with some formalized methods and functionality, which is geared towards receiving, sending, and processing data in small pieces called chunks. The type of stream, that is, whether it's readable, writable, or both, determines these methods and functionality. This is known as a duplex stream.
There are many advantages of streams over the more traditional buffering method, whereby all data is read into memory prior to processing. Primarily, we use less memory this way—once a chunk is processed and sent somewhere else, to a client for instance, and we no longer need that data, we can simply discard it. This allows the data to be garbage collected.
In addition, we can deliver this first chunk (and the subsequent chunks...