您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Node.js将同一可读流传输到多个(可写)目标中

Node.js将同一可读流传输到多个(可写)目标中

您必须通过将其输送到两个流中来创建流的副本。您可以使用PassThrough流创建简单流,只需将输入传递到输出即可。

const spawn = require('child_process').spawn;
const PassThrough = require('stream').PassThrough;

const a = spawn('echo', ['hi user']);
const b = new PassThrough();
const c = new PassThrough();

a.stdout.pipe(b);
a.stdout.pipe(c);

let count = 0;
b.on('data', function (chunk) {
  count += chunk.length;
});
b.on('end', function () {
  console.log(count);
  c.pipe(process.stdout);
});

输出

8
hi user
Node 2022/1/1 18:17:50 有494人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶