9951 explained code solutions for 126 technologies


nodejsHow to read stream to string


const fs = require('fs');
let input = fs.createReadStream('/var/www/examples/test.txt');

const chunks = [];
input.on('data', buf => chunks.push(buf));
input.on('end', () => console.log( Buffer.concat(chunks).toString() ))ctrl + c
require('fs')

library to work with file system

fs.createReadStream(

create stream to read data from (file in our case)

/var/www/examples/test.txt

path to file to stream read

.on('data'

handle date reading from stream

chunks.push(buf)

push each chunk into array

Buffer.concat(chunks)

join all chunks into single buffer

.toString()

convert Buffer to string

input.on('end'

fire when stream reading was finished


Usage example

const fs = require('fs');
let input = fs.createReadStream('/var/www/examples/test.txt');

const chunks = [];
input.on('data', buf => chunks.push(buf));
input.on('end', () => console.log( Buffer.concat(chunks).toString() ))
output
hi!