pipe道到标准输出和可写入的stream
我正在通过一个双工string( 通过礼貌)pipe道文件,我无法打印信息stdout
和写入文件。 一个或另一个工作得很好。
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! reader.pipe(catify).pipe(writer) // blank file. T_T reader.pipe(catify).pipe(process.stdout).pipe(writer)
我假设这是因为process.stdout
是一个可写的stream,但我不知道如何做我想要的(我试过传递{end: false}
无济于事)。
仍然努力把我的头围绕溪stream,所以原谅我,如果我错过了明显的东西:)
我想你想要的是:
reader.pipe(catify) catify.pipe(writer) catify.pipe(process.stdout)
这些需要分开,因为pipe道返回的目的地,而不是他们的来源。