Javascript nodejs将流管道连接到已连接的N个流中
我想通过管道将一个流传输到N个已经通过管道传输的流,下面的代码只返回Javascript nodejs将流管道连接到已连接的N个流中,javascript,node.js,stream,Javascript,Node.js,Stream,我想通过管道将一个流传输到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, en
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'});
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'});