Node.js 无法使用转换流创建流管道
这是我的代码:Node.js 无法使用转换流创建流管道,node.js,Node.js,这是我的代码: stream = require 'stream' stringer = new stream.Transform objectMode: true stringer2 = new stream.Transform objectMode: true stringer.push 'hello' stringer.push 'world' stringer.push null stringer2._transform = (chunk, enc, done) ->
stream = require 'stream'
stringer = new stream.Transform objectMode: true
stringer2 = new stream.Transform objectMode: true
stringer.push 'hello'
stringer.push 'world'
stringer.push null
stringer2._transform = (chunk, enc, done) ->
console.log chunk.toString()
done()
return @
stringer.pipe(stringer2).pipe process.stdout
我可以在
stringer2
中记录每个块,但我无法将结果传送到stdout
。如何执行此操作?您需要调用this.push(chunk)
将转换后的块传递到下一个管道。您也可以省略返回此,尽管它不会造成任何伤害
在coffeescript中,它将如下所示:
stringer2._transform = (chunk, enc, done) ->
console.log chunk.toString()
@push chunk
done()
或者,作为快捷方式,您可以将区块作为第二个参数传递到done()
(第一个参数是错误,如果没有错误,则为null):
你能在plunker/fiddle上复制这个吗?AFAIK codepen/fiddle/plunkr不能运行nodejs剪报。。。
stringer2._transform = (chunk, enc, done) ->
console.log chunk.toString()
done null, chunk