Welcome to this in-depth guide on the Stream Module in Node.js! By the end of this tutorial, you'll have a solid understanding of streams and how to use them in your projects. 📝 Note: This tutorial is designed for both beginners and intermediates, so we'll cover the topic from the ground up.
Streams are a fundamental concept in Node.js for handling data in a continuous flow, such as reading a file or sending HTTP responses. They allow us to process data in chunks, which is particularly useful for handling large amounts of data.
There are four main types of streams in Node.js:
http.request() and http.createServer().Let's create a simple readable stream by reading a file.
const fs = require('fs');
const readableStream = fs.createReadStream('example.txt');
readableStream.on('data', chunk => {
console.log(`Received data: ${chunk}`);
});In this example, we're using the built-in fs (File System) module to create a readable stream on a file named example.txt. The on('data') event is triggered whenever new data is available, and we're logging the received data to the console.
Now let's create a writable stream and write some data to a file.
const fs = require('fs');
const data = 'Hello, World!';
const writableStream = fs.createWriteStream('output.txt');
writableStream.write(data, err => {
if (err) throw err;
writableStream.end();
});Here, we're creating a writable stream to write the string 'Hello, World!' to a file named output.txt. We use the write() method to write the data and end() to signal that we're done writing.
One of the most powerful features of streams is the ability to pipe data from one stream to another. This allows for seamless data flow between streams.
const fs = require('fs');
const readableStream = fs.createReadStream('example.txt');
const writableStream = fs.createWriteStream('output.txt');
readableStream.pipe(writableStream, { end: false });
readableStream.on('end', () => {
writableStream.end();
});In this example, we're piping the data from the readable stream to the writable stream. Since we set end: false, the readable stream will continue to emit data until it's empty. Once the readable stream ends, we manually call end() on the writable stream.
What are the four main types of streams in Node.js? (Separate each with a comma)
Continue exploring streams in Node.js, and soon you'll be able to handle data like a pro! 🚀 Stay tuned for more lessons on Node.js here at CodeYourCraft.