What is Piping in Node.js ?
Last Updated :
12 Jun, 2024
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.
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.
There are four types of streams in Node.js:
- Readable Streams: Used for reading data (e.g.,
fs.createReadStream( )
). - Writable Streams: Used for writing data (e.g.,
fs.createWriteStream( )
). - Duplex Streams: Can be used for both reading and writing data (e.g., TCP sockets).
- Transform Streams: Duplex streams that can modify or transform the data as it is read and written (e.g.,
zlib.createGzip()
).
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.
readableStream.pipe(writableStream);
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:
const fs = require('fs');
const readableStream = fs.createReadStream('source.txt');
const writableStream = fs.createWriteStream('destination.txt');
readableStream.pipe(writableStream);
writableStream.on('finish', () => {
console.log('Data has been written successfully.');
});
In this example, data from source.txt
is read using a readable stream and written to destination.txt
using a writable stream.
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:
const http = require('http');
const fs = require('fs');
const server = http.createServer((req, res) => {
const readableStream = fs.createReadStream('source.txt');
res.writeHead(200, { 'Content-Type': 'text/plain' });
readableStream.pipe(res);
});
server.listen(3000, () => {
console.log('Server is running on port 3000');
});
In this example, when a request is made to the server, it reads data from source.txt
and pipes it directly to the response object, which is a writable stream.
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.
GeeksForGeeks 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.
GeeksForGeeks 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.
GeeksForGeeks 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 createdConclusion
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.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read