Complete the code to read a file using the built-in Node.js module.
const fs = require('fs'); fs.[1]('example.txt', (err, data) => { if (err) throw err; console.log(data.toString()); });
The readFile method reads the entire file into memory before calling the callback.
Complete the code to create a readable stream from a file.
const fs = require('fs'); const stream = fs.[1]('example.txt'); stream.on('data', chunk => { console.log(chunk.toString()); });
createReadStream creates a stream to read the file piece by piece.
Fix the error in the code to properly handle stream errors.
const fs = require('fs'); const stream = fs.createReadStream('example.txt'); stream.on('data', chunk => { console.log(chunk.toString()); }); stream.on('[1]', err => { console.error('Error:', err); });
The error event is emitted when the stream encounters an error.
Fill both blanks to pipe a readable stream into a writable stream.
const fs = require('fs'); const readable = fs.createReadStream('input.txt'); const writable = fs.[1]('output.txt'); readable.[2](writable);
createWriteStream creates a writable stream, and pipe connects the readable stream to it.
Fill all three blanks to read a file stream and count chunks received.
const fs = require('fs'); const stream = fs.[1]('data.txt'); let count = 0; stream.on('[2]', chunk => { count += 1; console.log(`Chunk #$[3] received`); });
createReadStream creates the stream, data event fires for each chunk, and count tracks the number of chunks.
