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.
Section overview
5 resourcesExplores 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.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI 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 articleAI 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 articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article