我正在通过双工字符串(由 through 提供)传输文件,并且无法将信息打印到 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
是一个可写流,但我不确定如何做我想做的事(我试过传递 {end: false }
无济于事)。
仍在努力将我的头包裹在溪流中,如果我错过了一些明显的东西,请原谅我:)
最佳答案
我想你想要的是:
reader.pipe(catify)
catify.pipe(writer)
catify.pipe(process.stdout)
这些需要分开,因为管道返回它们的目的地而不是它们的源。
关于node.js - 管道到标准输出和可写流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17822437/