Skip to content

Instantly share code, notes, and snippets.

@jtbonhomme
Created September 13, 2016 14:36
Show Gist options
  • Select an option

  • Save jtbonhomme/842be13d57ad6e852657d88c681755ec to your computer and use it in GitHub Desktop.

Select an option

Save jtbonhomme/842be13d57ad6e852657d88c681755ec to your computer and use it in GitHub Desktop.
This gist shows how to fork a nodejs stream into 2 streams
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