2014-04-04 41 views
1

我正在学习使用流 - 冒险节点流(https://github.com/substack/stream-adventure)。 我很难理解HTML流课程。了解节点流操作序列

这是挑战:

你的程序将获得一些HTML编写到标准输入。对于类名为“loud”的元素,将所有 内部html转换为大写。

您可以使用trumpetthrough来解决这个冒险。

我找到了解决办法here

var trumpet = require('trumpet'); 
var through = require('through'); 
var to_upper = function (buffer) { 
    this.queue(buffer.toString().toUpperCase()) 
}; 
var tr = trumpet(); 

// Here I expect the output to stdout to have printed as is. 
process.stdin.pipe(tr).pipe(process.stdout); 

// In the below lines, there is no reference to the above stream 
// which should have already started to send to stdout. 
// How are the lines below modifying the above stream? 
var stream = tr.select('.loud').createStream() 
stream.pipe(through(to_upper)).pipe(stream) 

我似乎无法理解程序流上面。

即使没有引用/回调使用上述流,最后两行如何修改流输出?

+0

我不熟悉它,但不会'tr'已经管理到process.stdin? – webduvet

+0

有一个[类似的问题](http://stackoverflow.com/questions/24103981/how-does-piping-a-stream-back-to-itself-work-with-trumpet),和[讨论]( https://github.com/nodeschool/discussions/issues/346) –

回答

0

关键是要异步思考。将process.stdin.pipe(tr).pipe(process.stdout)想象为“通过tr进行输入,稍后我们将详细介绍,然后打印到stdout”。

然后在下面我们填写逻辑并确切地说tr将对输入做什么。该程序中只有一个tr对象。并且stream对象是根据它定义的。在底部使用stream的代码操纵用于过滤stdinstdout的相同tr。 记住提示现在stream输出所有的内部html内容'.beep'和您写入stream的数据将显示为新的内部html内容。这正是最后一行所做的。