Compression is the process that decreases the file sizes. During the compression process all details from the original file will be maintained in compressed file. It boosts the working of Node.js applications by decreasing above 70 % of size.
Introduction:
Gzip is a file format and software application which is used in Unix systems for the compression of the HTTP file and shrinking files by compression algorithms. Don’t forget to decompress the code before our use.
Web servers are capable of covering more details about the searching object by adding headers with every HTTP requests. The accept encoding option will conclude which compression algorithm will be supported while the request header sent in by the browser.
Even there is a lot of text compression algorithms, only three supported algorithms are available for the compression and decompression of the HTTP requests.
- Deflate
- Brotli
- Gzip
Gzip is the most using compression format because of it’s suitability with every browsers . It is based on the deflate algorithm . There are two important compression methods which are listed below
- Middleware
- Proxy
In this vlog we will look up to the Middleware
Tools Required:
The node.js along with the npm package manager has to be installed for the downloading of the compression package.
Node should be equal to or greater than version 8.0 and the npm as equal to or greater than version 5.0 .
Creation Of File Without Compression
npm init
The command which is given above is the one using for generation of basic configuration file named as package.json
.Now we will need to give some details in there to start the work.
Else we can use a distinct way for the creation of the file as:
// package.json
{
"name": "nodejs-compression",
"version": "0.0.1",
"description": "Gzip compression with Node.js",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Víctor Valencia Rico",
"license": "ISC"
}
Next step we have to do is to install Express.js
with express package
npm i express --save
So now we can develop the server.js
file including the given code below:
// server.js
const express = require('express');
const app = express();
app.get('/', (req, res) => {
const animal = 'elephant';
// It will repeatedly send the word 'elephant' in a
// 'text/html' format file
res.send(animal.repeat(1000));
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
To check the result of the application enter to http://localhost:3000 using browser.
Here a text/html format file with 1000times printed word elephant is going to be directed by a GET operation and when we look up to the weight of answer which is send by the server will be around 8kB without the compression.
File With Gzip Compression
For the compression in our node.js application we will apply the compression middleware in main file of our application. It causes for the diminishing of server responses as smaller by enabling the gzip.
Now installment of package for compression takes place:
npm i compression --save
Modify our code as given below:
// server.js
const compression = require('compression');
const express = require('express');
const app = express();
// Compress all HTTP responses
app.use(compression());
app.get('/', (req, res) => {
const animal = 'elephant';
// It will repeatedly send the word 'elephant' in a
// 'text/html' format file
res.send(animal.repeat(1000));
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
Check by running the application:
Type http://localhost:3000 in browser and open. If the compression is turned on then the answer from the server will be weighted only 336 B. It means that 96% savings in the final result.
Extra Modifications
We can also modify the compression as we like , we can use some of different properties which we can select from ,see the documentation here. The following code shows the addition of option to the compression:
// server.js
const compression = require('compression');
const express = require('express');
const app = express();const shouldCompress = (req, res) => {
if (req.headers['x-no-compression']) {
// Will not compress responses, if this header is present
return false;
}
// Resort to standard compression
return compression.filter(req, res);
};
// Compress all HTTP responses
app.use(compression({
// filter: Decide if the answer should be compressed or not,
// depending on the 'shouldCompress' function above
filter: shouldCompress,
// threshold: It is the byte threshold for the response
// body size before considering compression, the default is 1 kB
threshold: 0
}));
app.get('/', (req, res) => {
const animal = 'elephant';
// It will repeatedly send the word 'elephant' in a
// 'text/html' format file
res.send(animal.repeat(1000));
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
So we completed the compression with the customization as suitable for us by the modification of code.