Compress Payload in NodeJS

Compress Payload (response) in NodeJS


Compression in Node.js and Express.js decreases the amount of downloadable data from a website or app. By using this compression, we can improve the performance of our Node.js applications as our payload size is dramatically reduced above 70%.

Compression generally refers to code that has been modified using a data compression algorithm. Unlike minification that ends up providing perfectly valid code, compressed code must be unzipped before use.


There are many text compression algorithms, but there are only 3 supported algorithms for compression (and decompression) of HTTP requests:

  • Deflate (deflate): Not commonly used.
  • Gzip (gzip): The most widely used compression format for server and client interactions. It is based on the Deflate algorithm and is compatible with all current browsers.
  • Brotli (br): A newer compression algorithm that aims to further improve compression ratios, which can result in even faster page loads. It is compatible with the latest versions of most browsers.

In addition, it is important to say that there are 2 compression methods:

  • Middleware: In this method we call it directly in our Node.js application, using the compression middleware called compression.
  • Proxy: In this method it is used at the level of a reverse proxy through NGINX or Apache.


In this blog, we will see Middleware compression using "compression".

Example without Compression


const express = require('express');

const app = express();


app.get('/',(req,res)=>{

    const text = "A big elephant is walking in a jungle.";
    res.send(text.repeat(1000));
});


app.listen(3000,()=>{
    console.log('App is running.');
});


When running this without compression, our results are:


So, without compression we have 38.2kB file.

Let's implement compression.

const compression = require('compression');
const express = require('express');

const app = express();

// all responses would be compression.
app.use(compression());

app.get('/',(req,res)=>{

    const text = "A big elephant is walking in a jungle.";
    res.send(text.repeat(1000));
});


app.listen(3000,()=>{
    console.log('App is running.');
});

Now, run the app and see results.


Significant difference! now file size is only 484 bytes.

We can apply filter and threshold so that we apply compression when need or on particular conditions.

const shouldCompress = (req,res) => {
    if (req.headers && req.headers['x-no-compression']){
       return false;
    }

    return compression.filter(req,res)
};

app.use(compression({

    filter: shouldCompress,
    threshold:0

}));

app.get('/',(req,res)=>{

    const text = "A big elephant is walking in a jungle.";
    res.send(text.repeat(1000));
});

We have applied filter method and threshold = 0. Default value of threshold is 1kB.

So this way, we can achieve compression in NodeJS application.









Comments

Popular posts from this blog

Data Bound Controls in ASP.Net - Part 4 (FormView and DetailsView controls)

ASP.net: HttpHandlers

The Clickjacking attack and X-Frame-Options