Node.js / Node.js Streams and Buffers

Working with Readable and Writable Streams

In this tutorial, we'll dive deeper into Readable and Writable streams. You'll learn how to read data from sources and write data to destinations using these types of streams.

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Explores working with streams and buffers for handling large data efficiently.

1. Introduction

1.1 Tutorial's Goal

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.

1.2 Learning Outcomes

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

1.3 Prerequisites

Familiarity with JavaScript and basic knowledge of Node.js would be beneficial for this tutorial.

2. Step-by-Step Guide

2.1 Understanding Streams

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.

2.2 Readable Streams

A Readable stream is an abstraction for a source from which data is consumed. Examples include reading data from a file.

2.2.1 Reading from a Stream

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.

2.3 Writable Streams

A Writable stream is an abstraction for a destination where data is written. Examples include writing data to a file.

2.3.1 Writing to a Stream

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.

3. Code Examples

3.1 Reading from a File with a Readable 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.

3.2 Writing to a File with a Writable Stream

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.

4. Summary

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.

4.1 Next Steps

To continue learning about streams, start exploring Duplex and Transform streams.

4.2 Additional Resources

5. Practice Exercises

5.1 Exercise 1: Reading from a File

Read data from a file using a Readable stream and count the number of words in the file.

5.2 Exercise 2: Writing to a File

Write a program that takes user input from the console and writes it to a file using a Writable stream.

5.3 Exercise 3: Copy a File

Write a program that reads from one file and writes to another, effectively copying the file.

Solutions

Solution to Exercise 1

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}`);
});

Solution to Exercise 2

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();
});

Solution to Exercise 3

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!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Date Difference Calculator

Calculate days between two dates.

Use tool

Lorem Ipsum Generator

Generate placeholder text for web design and mockups.

Use tool

Hex to Decimal Converter

Convert between hexadecimal and decimal values.

Use tool

Word Counter

Count words, characters, sentences, and paragraphs in real-time.

Use tool

Text Diff Checker

Compare two pieces of text to find differences.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help