How to do compression with Gzip in Node.js ?

28 Apr, 2021
The following approach covers how to do compression with Gzip compression in Node.js. We can use the compression module in order to do compression with Gzip compression. Gzip is the most widely used compression, especially for server and client interactions.
Compression helps in decreasing the amount of downloadable data in our NodeJS application, and it leads to an improvement in the performance of the application. This compression process can reduce the payload size dramatically above 70%.
The following two examples cover how the compression is useful as it reduces the payload size as clearly visible in example 2 as compared to example 1.
Example 1: Using Without Compression:
Step 1: Create an empty NodeJS application with the following command:
mkdir Project 
cd Project
npm init -y
Step 2: Install the ExpressJS module using the following command:
npm i express --save
Step 3: Create a file on your project’s root directory and name index.js and write the following code. 
index.js
const express = require('express');const app = express();  // It will repeatedly the word 'I love GeeksforGeeks'const data = ('I love GeeksforGeeks').repeat(800) ;  app.get('/', (req, res) => {  // Send as 'text/html' format file  res.send(data);});  // Server setupapp.listen(8080, function () {  console.log('Server listening on port 8080!');});
Run the index.js file using the following command:
node index.js
Open the browser and go to the URL http://localhost:8080/, Without compression, the response returned by the server will weigh around 16 kB, as shown below:
Example 2: Using Gzip Compression:
Add compression middleware in the index.js file of your NodeJS application. This will enable GZIP which makes your HTTP responses smaller.
Step 1: Create an empty NodeJS application with the following command:
mkdir Project
cd Project
npm init -y
Step 2: Install the compression and ExpressJS module using the following command:
npm install compression --save
npm install express --save
Step 3: Create a file on your project’s root directory and name index.js and write the following code. 
index.js
// Initialize compression moduleconst compression = require('compression');const express = require('express');const app = express();  // Compress all HTTP responsesapp.use(compression());  // It will repeatedly the word 'I love GeeksforGeeks'const data = ('I love GeeksforGeeks').repeat(800) ;  app.get('/', (req, res) => {  // Send as 'text/html' format file  res.send(data);});  // Server setupapp.listen(8080, function () {  console.log('Server listening on port 8080!');});
Run the index.js file using the following command:
node index.js
Open the browser and go to the URL http://localhost:8080/, Now, if compression is turn on the response returned by the server will weigh around 371 bytes, as shown below:
So, the above example shows that using compression response size is reduced dramatically. 
NodeJS-Questions
Picked
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
JWT Authentication with Node.js
Installation of Node.js on Windows
Difference between dependencies, devDependencies and peerDependencies
Mongoose Populate() Method
Mongoose find() Function
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?
Differences between Functional Components and Class Components in React