繁体   English   中英

nodejs流管道到已经管道的N个流中

[英]nodejs stream pipe into already piped N streams

我想将一个流传输到N个已经传输的流,下面的代码仅返回here2它不在第一个转换流中。 似乎管道流的行为不像一个流。

'use strict';

var stream = require('stream');
var through2 = require('through2');

var pass = new stream.PassThrough({objectMode: true});

var transform1 = through2.obj(function(obj, enc, done) {
    console.log('here1');
    this.push(obj);
    done();
}).pipe(through2.obj(function(obj, enc, done) {
    console.log('here2');
    this.push(obj);
    done();
}));

pass.write({'hello': 'world'});

pass.pipe(transform1).on('data', function(data) {
    console.log(data);
});

pipe方法返回目标流。 因此transform1流实际上是管道链中的第二个流。 最终,您只将pass流写入第二个流(因此输出始终为“ here 2”):尝试以下操作:

pass.pipe(through2.obj(function(obj, enc, done) {
    console.log('here1');
    this.push(obj);
    done();
})).pipe(through2.obj(function(obj, enc, done) {
    console.log('here2');
    this.push(obj);
    done();
}));

pass.write({'hello': 'world'});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM