Nic*_*lin 18 node.js node.js-stream
我正在通过双工字符串管道文件(礼貌地通过),我无法将信息打印到文件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)
Run Code Online (Sandbox Code Playgroud)
我假设这是因为process.stdout
是一个可写的流,但我不知道如何做我想要的(我试过传递{end: false}
无济于事).
仍在努力将我的头包裹在溪流周围,所以请原谅我,如果我错过了一些明显的东西:)
Jon*_*Ong 28
我想你想要的是:
reader.pipe(catify)
catify.pipe(writer)
catify.pipe(process.stdout)
Run Code Online (Sandbox Code Playgroud)
这些需要分开,因为管道返回目的地而不是它们的来源.
归档时间: |
|
查看次数: |
28234 次 |
最近记录: |