How do I collect all the data from a Node.js stream into a string?
(This answer is from years ago, when it was the best answer. There is now a better answer below this. I haven't kept up with node.js, and I cannot delete this answer because it is marked "correct on this question". If you are thinking of down clicking, what do you want me to do?)
The key is to use the data
and end
events of a Readable Stream. Listen to these events:
stream.on('data', (chunk) => { ... });
stream.on('end', () => { ... });
When you receive the data
event, add the new chunk of data to a Buffer created to collect the data.
When you receive the end
event, convert the completed Buffer into a string, if necessary. Then do what you need to do with it.