Nick Tomlin Nick Tomlin - 1 year ago 92
Node.js Question

Pipe to stdout and writeable stream

I'm piping a file through a duplex string (courtesy of through) and I'm having trouble printing information to

and writing to the file. One or the other works just fine.

var fs = require('fs');
var path = require('path');
var through = require('through'); // easy duplexing, i'm young

catify = new through(function(data){
this.queue(data.toString().replace(/(woof)/gi, 'meow'));

var reader = fs.createReadStream('dogDiary.txt'); // woof woof etc.
var writer = fs.createWriteStream(path.normalize('generated/catDiary.txt')); // meow meow etc.

// yay!

// blank file. T_T

I'm assuming this is because
is a writeable stream, but I'm not sure how to do what I want (i've tried passing
{end: false}
to no avail).

Still struggling to wrap my head around streams, so forgive me if i've missed something obvious : )

Answer Source

I think what you want is:


These needed to be separated because pipes return their destinations and not their source.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download