Created
September 13, 2016 14:36
-
-
Save jtbonhomme/842be13d57ad6e852657d88c681755ec to your computer and use it in GitHub Desktop.
This gist shows how to fork a nodejs stream into 2 streams
This file contains hidden or 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