【问题标题】:nodejs stream pipe into already piped N streamsnodejs流管道进入已经管道的N个流
【发布时间】:2015-08-03 08:55:05
【问题描述】:

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

【问题讨论】:

    标签: javascript node.js stream


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-19
      • 2022-01-03
      • 2017-04-21
      • 1970-01-01
      • 2016-12-15
      • 2013-03-28
      • 1970-01-01
      • 2011-04-10
      相关资源
      最近更新 更多