Piping stream to a variable!? or ....? Piping stream to a variable!? or ....? express express

Piping stream to a variable!? or ....?


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