This tutorial aims to provide a comprehensive understanding of Readable and Writable streams in Node.js. It will give you the skills to read data from sources and write data to destinations leveraging these types of streams.
By the end of this tutorial, you will:
- Understand the concepts of Readable and Writable streams
- Be able to read from and write to streams
- Learn how to handle stream events
Familiarity with JavaScript and basic knowledge of Node.js would be beneficial for this tutorial.
In Node.js, Streams are objects that let you read data from a source or write data to a destination in a continuous fashion. There are four types of streams - Readable, Writable, Duplex (can read and write), and Transform (can modify the data while reading and writing).
This tutorial focuses on Readable and Writable streams.
A Readable stream is an abstraction for a source from which data is consumed. Examples include reading data from a file.
Node.js provides the fs.createReadStream()
method to read from a file. This method returns a new Readable stream.
const fs = require('fs');
let readableStream = fs.createReadStream('file.txt');
readableStream.on('data', function(chunk) {
console.log(chunk.toString());
});
In the example above, we are listening for the 'data' event. When this event is fired, we log the chunk of data read from the file.
A Writable stream is an abstraction for a destination where data is written. Examples include writing data to a file.
Node.js provides the fs.createWriteStream()
method to write to a file. This method returns a new Writable stream.
const fs = require('fs');
let writableStream = fs.createWriteStream('file.txt');
writableStream.write('Hello, world!\n');
writableStream.end('End of write stream.');
Here, we're using the write()
method to write data to the stream. The end()
method signifies that no more data will be written to the stream.
const fs = require('fs');
let readableStream = fs.createReadStream('input.txt');
readableStream.on('data', function(chunk) {
console.log(`Received ${chunk.length} bytes of data.`);
});
readableStream.on('end', function() {
console.log('There will be no more data.');
});
In this example, we're reading from a file named 'input.txt'. We're listening for two events: 'data' and 'end'. With each 'data' event, we log the length of the received chunk. When the 'end' event fires, we log a message indicating there's no more data.
const fs = require('fs');
let writableStream = fs.createWriteStream('output.txt');
writableStream.write('Hello, world!\n');
writableStream.end('End of write stream.');
writableStream.on('finish', function() {
console.log('All writes are now complete.');
});
In this example, we're writing to a file named 'output.txt'. We're also listening for the 'finish' event, which is emitted when all data has been flushed to the underlying system.
In this tutorial, we learned about Readable and Writable streams in Node.js. We learned how to read from and write to streams and handle stream events.
To continue learning about streams, start exploring Duplex and Transform streams.
Read data from a file using a Readable stream and count the number of words in the file.
Write a program that takes user input from the console and writes it to a file using a Writable stream.
Write a program that reads from one file and writes to another, effectively copying the file.
const fs = require('fs');
let wordCount = 0;
let readableStream = fs.createReadStream('input.txt');
readableStream.on('data', function(chunk) {
let words = chunk.toString().split(' ');
wordCount += words.length;
});
readableStream.on('end', function() {
console.log(`Word count: ${wordCount}`);
});
const fs = require('fs');
const readline = require('readline');
let writableStream = fs.createWriteStream('output.txt');
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter a line to write to the file: ', function(input) {
writableStream.write(input + '\n');
rl.close();
writableStream.end();
});
const fs = require('fs');
let readableStream = fs.createReadStream('input.txt');
let writableStream = fs.createWriteStream('output.txt');
readableStream.on('data', function(chunk) {
writableStream.write(chunk);
});
readableStream.on('end', function() {
console.log('Finished copying file.');
});
You can practice these exercises to strengthen your understanding of streams. Happy coding!