Node.js Stream Module Tutorial 🎯

beginner
25 min

Node.js Stream Module Tutorial 🎯

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.

What are Streams? 📝

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.

Stream Types 📝

There are four main types of streams in Node.js:

  1. Readable Streams: Used for reading data, like reading from a file or network connection.
  2. Writable Streams: Used for writing data, like writing to a file or network connection.
  3. Duplex Streams: Both readable and writable, like the http.request() and http.createServer().
  4. Transform Streams: A specialized duplex stream that modifies the data being piped through it.

Creating Readable Streams 📝

Let's create a simple readable stream by reading a file.

javascript
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.

Creating Writable Streams 📝

Now let's create a writable stream and write some data to a file.

javascript
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.

Piping Streams 📝

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.

javascript
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.

Quick Quiz
Question 1 of 1

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.