How do I collect all the data from a Node.js stream into a string?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
(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
dataandendevents of a Readable Stream. Listen to these events:When you receive the
dataevent, add the new chunk of data to a Buffer created to collect the data.When you receive the
endevent, convert the completed Buffer into a string, if necessary. Then do what you need to do with it.