Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript nodejs将流管道连接到已连接的N个流中_Javascript_Node.js_Stream - Fatal编程技术网

Javascript nodejs将流管道连接到已连接的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

我想通过管道将一个流传输到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'});

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'});