Installation Steps
Step 1: Create a file named input.txt and paste the following text into it. Create a file name output.txt and keep it empty.
w3wiki is awesome.
Step 2: Create a file index.js. and paste the following code into it.
Project Structure:
Example: Basic implementation of Piping in Nodejs
// index.js
const fs = require("fs");
let rs = fs.createReadStream("./input.txt");
let ws = fs.createWriteStream("./output.txt");
function callback(msg) {
console.log(msg);
}
// pipeReadToWrite() accepts two streams a
// readStream and s writeStream and a callback function.
function pipeReadToWrite(readStream, writeStream, callback) {
// handles any error occurred in the stream
function handleError(err) {
// close the streams and call callback
readStream.close();
writeStream.close();
callback(err);
}
readStream
.on("error", handleError)
.pipe(writeStream)
.on("error", handleError)
.on("finish", callback);
}
pipeReadToWrite(rs, ws, callback);
let rs = fs.createReadStream("./input.txt");
let ws = fs.createWriteStream("./output.txt");
function callback(msg) {
console.log(msg);
}
// pipeReadToWrite() accepts two streams a
// readStream and s writeStream and a callback function.
function pipeReadToWrite(readStream, writeStream, callback) {
// handles any error occurred in the stream
function handleError(err) {
// close the streams and call callback
readStream.close();
writeStream.close();
callback(err);
}
readStream
.on("error", handleError)
.pipe(writeStream)
.on("error", handleError)
.on("finish", callback);
}
pipeReadToWrite(rs,ws,callback);
Explanation:
- Include the fs (file stream) module which is used to create and interact with file streams. Create a read stream and connect it to input.txt and a write stream and connect it to output.txt
- pipeReadToWrite() function accepts a read stream and a write stream.
- handleError() is a callback function that handles any error that occurs in reading or writing the stream.
- execute the pipeReadToWrite() function with the required arguments.
Step 3: Execute the index.js file by writing the command in the command line
node index.js
Output: Open output.txt and you will see the following output displayed in it.
w3wiki is awesome.
Approach 2: Connecting more than two streams to create a complex workflow
Project Structure:
The following modules are required in this approach.
- fs: file stream module used for creating and interacting with file streams.
- zlib: provides various methods to compress files.
Step 1: Create a file named input.txt and paste the following text into it. Create a file name output.txt and keep it empty.
w3wiki is awesome.
Step 2: Create a file index.js. and paste the following code into it.
const fs = require("fs");
const zlib = require("zlib");
// gzip() function accepts a filename
// to be compressed and a callback function
function gzip(filename, callback) {
// Create the streams
let source = fs.createReadStream(filename);
let destination = fs.createWriteStream(filename + ".gz");
let gzipper = zlib.createGzip();
// Set up the pipeline
source
.on("error", callback)
.pipe(gzipper)
.pipe(destination)
.on("error", callback)
.on("finish", callback);
}
gzip("./input.txt", (msg) => {
console.log(msg);
});
Explanation:
- Include all the required modules
- The function gzip() accepts a path to a file and a callback function. It compresses the file specified in the path and executes the callback function after successfully compressing the file.
- Execute the gzip() function.
Step 3: Execute the index.js file. Go to the command line and execute the command given below:
node index.js
Output: You will see a new file named input.txt.gz is created. This is the compressed version of input.txt.
What is Piping in Node.js ?
Piping in NodeJS is the process by which byte data from one stream is sent to another stream. It is a powerful feature in Node.js that allows data to be transferred from one stream to another seamlessly. Streams are an integral part of Node.js, providing a mechanism for handling data that is too large to be processed all at once or for real-time data processing.
Table of Content
- Understanding Streams
- What is Piping?
- Reading from a File and Writing to Another File
- HTTP Server with Piping
- Advantages of Using Piping
- Conclusion