node.js - 管道到标准输出和可写流

标签 node.js node.js-stream

我正在通过双工字符串(由 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/

相关文章:

css - 在使用 Node 进行 SASS 编译期间注入(inject)变量

node.js - Ejs input_field_tag方法设置type属性为5

string - Node.js - 如何将流转换为字符串

node.js - 如何等待所有 Node 流完成/结束?

node.js - 如何实现可写流

node.js - 如何在一对一关系上使用记录 ID 进行 .find()

使用实例名称、端口和域的 Node.js 连接到 SQL Server

node.js jade - 无法访问嵌套数组元素

r - 从 node.js 调用 Rscript