Open In App

How to do compression with Gzip in Node.js ?

Last Updated : 28 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 setup
app.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 module
const compression = require('compression');
const express = require('express');
const app = express();
  
// Compress all HTTP responses
app.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 setup
app.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. 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads