In this tutorial, we are going to explore the concepts of piping and chaining streams in Node.js. By the end of this tutorial, you will master how to efficiently manage data flow between streams.
You will learn:
Prerequisites:
Streams are collections of data that might not be available all at once. They let you handle data in chunks as it comes in or goes out, which is crucial for handling large amounts of data or real-time data.
Piping is a mechanism where we provide the output of one stream as the input to another stream. It is normally used to get data from one stream and pass the output of that stream to another stream. There is no limit on piping operations.
Here's an example of piping between a readable and writable stream:
const fs = require('fs');
let readableStream = fs.createReadStream('input.txt');
let writableStream = fs.createWriteStream('output.txt');
// pipe the read and write operations
// read input.txt and write data to output.txt
readableStream.pipe(writableStream);
console.log("Piping Finished");
In this example, we're reading data from input.txt
and writing it to output.txt
.
Chaining is a mechanism to connect the output of one stream to another stream and create a chain of multiple stream operations. It is normally used with piping operations. Let's look at an example:
const fs = require('fs');
const zlib = require('zlib');
// Compress the file input.txt to input.txt.gz
fs.createReadStream('input.txt')
.pipe(zlib.createGzip())
.pipe(fs.createWriteStream('input.txt.gz'));
console.log('File Compressed');
In this code, we're chaining .pipe()
calls. We're reading from input.txt
, compressing it using gzip (provided by the zlib module), and then writing it to input.txt.gz
.
This example copies the content from the source file to the destination file using piping.
const fs = require('fs');
// Create a readable stream
let readableStream = fs.createReadStream('source.txt');
// Create a writable stream
let writableStream = fs.createWriteStream('destination.txt');
// Pipe the two streams
readableStream.pipe(writableStream);
console.log("Piping Complete");
This script reads from source.txt
and pipes the output into destination.txt
.
This example reads data from a file, compresses it, and writes it to a new file.
const fs = require('fs');
const zlib = require('zlib');
// Create a readable stream
let readableStream = fs.createReadStream('uncompressed.txt');
// Create a writable stream
let writableStream = fs.createWriteStream('compressed.txt.gz');
// Create gzip object
let gzip = zlib.createGzip();
// Pipe the streams
readableStream.pipe(gzip).pipe(writableStream);
console.log("Chaining Complete");
This script reads from uncompressed.txt
, compresses the data, and writes the compressed data to compressed.txt.gz
.
In this tutorial, we covered the concept of piping and chaining streams in Node.js. We learned how to use piping to direct data from a readable stream to a writable stream, and how to use chaining to connect multiple operations together.
For further learning, consider exploring other stream methods like stream.write(data[, encoding][, callback])
, stream.end([data][, encoding][, callback])
, and events like data
, end
, and error
.
Exercise 1:
Create a script that copies content from one file to another. Use piping.
Solution:
const fs = require('fs');
let readStream = fs.createReadStream('file1.txt');
let writeStream = fs.createWriteStream('file2.txt');
readStream.pipe(writeStream);
console.log('Content copied!');
Exercise 2:
Create a script that reads a text file, converts the text to uppercase, and writes the result to a new file. Use piping and chaining.
Solution:
const fs = require('fs');
const through2 = require('through2');
let readStream = fs.createReadStream('lowercase.txt');
let writeStream = fs.createWriteStream('uppercase.txt');
readStream
.pipe(through2(function(chunk, _, next) {
this.push(chunk.toString().toUpperCase());
next();
}))
.pipe(writeStream);
console.log('Text converted to uppercase!');
In this solution, we are using the through2
module to handle the transformation. This module provides a simpler API for dealing with streams.