-
-
Save rhalff/e2176da1b2789736c9940671b899a9e6 to your computer and use it in GitHub Desktop.
This gist shows how to fork a nodejs stream into 2 streams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var spawn = require('child_process').spawn; | |
var pass = require('stream').PassThrough; | |
var a = spawn('echo', ['hi user']); | |
var b = new pass; | |
var c = new pass; | |
a.stdout.pipe(b); | |
a.stdout.pipe(c); | |
var count = 0; | |
b.on('data', function(chunk) { count += chunk.length; }); | |
b.on('end', function() { console.log('b stream : ' + count); }); | |
var data = ''; | |
c.on('data', function(chunk) { data += chunk; }); | |
c.on('end', function() { console.log('c stream : ' +data); }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment