How does node turn stream into buffer object?

  node.js, question

How does node turn stream into buffer object?

The problem is too short for me to understand. I will tell you what I think.

If you don’t have to pipe, you need to monitor your streamdataAndendEvents
The following is the read stream of one file, converted to buffer, and then written to other files

const readStream = fs.createReadStream(path.join(__dirname, 'xxx.txt'))
 const buffers = [];
 readStream.on('data', function(buffer) {
 buffers.push(buffer);
 });
 readStream.on('end', function() {
 const data = Buffer.from(buffers);
 //...do your stuff  ...
 
 // such as write to file:
 fs.writeFile('xxx.txt', data, function(err) {
 // handle error, return response, etc  ...
 });
 });
 readStream.on('error', function(error){
 console.error('readStream error:', error.message);
 })

However, it is best to recommend pip mode, so if the buffer read is very large, there will be pressure on the memory, and pip will not

const readStream = fs.createReadStream(path.join(__dirname, './xxx_read.json'))
 const writeStream = fs.createWriteStream(path.join(__dirname, './xxx_write.json'))
 readStream.pipe(writeStream)
 readStream.on('error', (error) => {
 console.log('readStream error', error.message);
 })
 writeStream.on('error', (error) => {
 console.log('writeStream error', error.message);
 })