How to pipe one readable stream into two writable streams at once in Node.js?

前端 未结 3 1311
轻奢々
轻奢々 2020-12-05 13:29

The goal is to:

  1. Create a file read stream.
  2. Pipe it to gzip (zlib.createGzip())
  3. Then pipe the read stream of zlib output to:

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-05 13:44

    Pipe chaining/splitting doesn't work like you're trying to do here, sending the first to two different subsequent steps:

    sourceFileStream.pipe(gzip).pipe(response);

    However, you can pipe the same readable stream into two writeable streams, eg:

    var fs = require('fs');
    
    var source = fs.createReadStream('source.txt');
    var dest1 = fs.createWriteStream('dest1.txt');
    var dest2 = fs.createWriteStream('dest2.txt');
    
    source.pipe(dest1);
    source.pipe(dest2);
    

提交回复
热议问题