admin管理员组

文章数量:1290959

What are my options to pipe stream to a variable? Based on this documentation the examples of writable stream include:

  • http requests, on the client
  • http responses, on the server fs write
  • streams zlib streams
  • crypto streams
  • tcp sockets
  • child process stdin
  • process.stdout, process.stderr

So does it mean I cannot pipe stream to a variable to process it? In fact I dont want to save the stream on my disk, so what is the best way to pipe all of the streams and use that data?

Thanks please let me know if you need more clarification!

What are my options to pipe stream to a variable? Based on this documentation the examples of writable stream include:

  • http requests, on the client
  • http responses, on the server fs write
  • streams zlib streams
  • crypto streams
  • tcp sockets
  • child process stdin
  • process.stdout, process.stderr

So does it mean I cannot pipe stream to a variable to process it? In fact I dont want to save the stream on my disk, so what is the best way to pipe all of the streams and use that data?

Thanks please let me know if you need more clarification!

Share Improve this question asked Jan 18, 2015 at 5:44 user3399784user3399784 4
  • 1 You can pipe a stream to a variable, but pipe is generally used to pipe the stream to some other method that can utilize the stream, if you just want the data in a variable there are events for that, like on('data') etc – adeneo Commented Jan 18, 2015 at 5:58
  • Thanks man! and is there any difference between on('data') and on('readable')? – user3399784 Commented Jan 18, 2015 at 6:11
  • 1 Both events are described in the documentation, but the difference is somewhat cryptic, readable fires when the stream is, well, readable, while data apparently fires when there's data. What the specific differences are, I don't know ! – adeneo Commented Jan 18, 2015 at 6:25
  • 1 Would you mind adding your answer and I mark it as an answer for future reference; it can be usefull for people who are new to Node.JS/JavaScript like me! And thanks again! – user3399784 Commented Jan 18, 2015 at 6:26
Add a ment  | 

1 Answer 1

Reset to default 8

You probably can pipe a stream to a variable, but pipe is generally used to pipe the stream to some other method that can utilize the stream, as pipe pulls all the data out of a readable stream, and writes it to the supplied destination, automatically managing the flow so that the destination is not overwhelmed by a fast readable stream.

For instance, piping a stream to a file

someReadableStream.pipe(fs.createWriteStream("result.json"));

If you just want the data in a variable, there are events for that, that are probably easier to use, like on('data')

var readable = getReadableStreamSomehow(),
    result   = '';

readable.on('data', function(chunk) {
      result += chunk;
});

readable.on('end', function () {
    // do something with "result"
});

本文标签: javascriptPiping stream to a variable or Stack Overflow