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

Node
// 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.

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

input.txt.gz file is created

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

Similar Reads

Understanding Streams

Before diving into piping, it is essential to understand what streams are. Streams in Node.js are objects that allow reading or writing of data in a continuous manner. They are particularly useful for handling I/O operations efficiently, such as reading from a file, writing to a file, or sending/receiving data over a network....

What is Piping?

Piping in Node.js is a method used to connect a readable stream to a writable stream, allowing data to flow automatically from the source to the destination. The pipe() method is used to achieve this, making it easy to handle data streaming operations with minimal code....

Reading from a File and Writing to Another File

A common use case for piping is reading data from one file and writing it to another. Here’s a simple example using the built-in fs module:...

HTTP Server with Piping

Piping is also useful in networking. Here’s an example of creating an HTTP server that serves a file using streams and piping:...

Advantages of Using Piping

Efficient Memory Usage: Piping uses streams, which handle data in chunks, making it more memory efficient than reading or writing large amounts of data at once.Simplicity and Conciseness: The pipe() method simplifies code by reducing the need for manual data handling and event listening.Real-time Data Processing: Piping facilitates real-time data processing, enabling operations like on-the-fly compression, encryption, and transformation....

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

Conclusion

Piping in Node.js is a powerful and efficient way to handle data streaming operations. By connecting readable and writable streams, piping allows for seamless data transfer and transformation, making it an essential feature for building high-performance, scalable applications. Whether you are handling file I/O, network communication, or real-time data processing, understanding and leveraging piping can significantly enhance your Node.js development skills....