Compress HTTP Request and Response in Node.js Server

Nodejs compression

Node.js is taking over the API development zone and knocking every competitor out, but this is not the only place where you can use Node.js server.

Some of the big companies like Uber, Paypal, and Housing are using Node.js as a front-end server. When you deal with front-end and browser, you need to make sure the speed is up to the standard.

The necessary and essential way to make sure speed is optimal is by using compression. In this post, we are going to look at how to compress the request and response of Node.js Server to improve the website speed.

Compression algorithms

There are two types of compression:

  • Lossless
  • Lossy

As the name suggests, we need to focus on compression, which should not lose or corrupt the data. In other words, we need to focus on Lossless compressions.

There are two popular lossless compression algorithms for HTTP:

  • Gzip
  • Deflate

Gzip is also file format and one of the standard ways (RFC 2616) to compress HTTP data.

Compression in Node.js

Let’s write a program to compress HTTP requests and response.

Create a new project.

npm init --y

Install the required dependency.

npm i --S express compression

Here is our app.js.

var compression = require('compression')
var express = require('express')
var router = express.Router();
var app = express()
// compress all requests
// add all routes
router.get('*',(req,res) => {

Hello World


app.listen(process.env.PORT || 3000);

Run the program and hit the request from the browser. Every request now would be compressed.

Check out the npm package for more details.


This is a short tip to compress HTTP request and response. You can also compress images and files using standard algorithms in Node.js. There are plenty of node modules available for such purpose as well.

11 thoughts on “Compress HTTP Request and Response in Node.js Server”

  1. Alexandre Emeriau

    “Node.js is taking over the API development zone and knocking every competitor out,” I wish it would be true but there are domains where it is not even considered, think of stock exchange, spread betting for example, c# is king there, no one talk about node.js.

    1. Shahid (UnixRoot) Shaikh

      If you check out recent Stack Overflow reports, Server side JavaScript is taking over rapidly. The majority of the big companies are indeed using C# and other tech but with the stability of the Node base module, I am sure they will start to use it as well.

  2. Thanks for the post, always great info! Just a quick note, this fails above with res undefined:
    router.get(‘*’,() => {
    res.write(‘Hello World’);

    Should be

    router.get(‘*’,(req, res) => {
    res.write(‘Hello World’);

  3. imho, NodeJS is loads better than C# in terms of asynchronous processing and code readability.

  4. I keep getting a syntax error on this. It is pointing to my left brace on the router.get call. Any ideas? Code is below.

    var compression = require(‘compression’);
    var express = require(‘express’);
    var router = express.Router();
    var app = express();

    // compress all requests

    // add all routes
    router.get(‘*’,(req, res) => {
    res.write(‘Hello World’);

    app.use(“/”, router);
    app.listen(process.env.PORT || 3000);

      1. I am using whatever the current recommended version is when you go to the node js website. 6. Something.

  5. I don’t think this does what you think it does…

    // compress all requests — <-this is wrong, should be responses, and only when you send the accept-encoding header.

    You aren't compressing requests you are compressing responses.

  6. In response if i am passing everything with in an object, the compress will work. and if this works, how can i decompress the data on front end. either if you can give example with front end also, it might be helpfull

    1. Shahid (UnixRoot) Shaikh

      You don’t need to decompress at front-end. Gzip works on TCP/IP Layer hence browser does the job of decryption for you.

Comments are closed.