Playing with pipes
A pipe is used to connect streams together. DOS and Unix-like shells use the vertical bar (|
) to pipe the output of one program to another; we can chain several pipes together to process and massage data in number of ways. Likewise, the Streams API affords us the pipe
method to channel data through multiple streams. Every readable stream has a pipe
method that expects a writable stream (the destination) as its first parameter.
As in the Consuming streams recipe, we're going to receive a stream from the Couch database, which backs the npm registry, and display it in the terminal; only this time we'll be using pipes instead of the read
method.
Getting ready
Let's create a file named npm_stream_piper.js
.
How to do it…
Let's set up some initial variables as shown in the following code:
var http = require('http'), feed = 'http://isaacs.iriscouch.com/registry/_changes?feed=continuous';
Implement the decide
function (as in the Consuming streams recipe) with the following code:
function...