Nodejs compression

Compress HTTP Request and Response in Node.js Server

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.

app.js
var compression = require('compression')
var express = require('express')
var router = express.Router();
var app = express()
 
// compress all requests
app.use(compression())
 
// add all routes
router.get('*',(req,res) => {
    res.write('<h1>Hello World</h1>');
});

app.use('/',router);
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.

Summary

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.

Shahid (UnixRoot) Shaikh

Hey there, This is Shahid, an Engineer and Blogger from Bombay. I am also an Author and i wrote a programming book on Sails.js, MVC framework for Node.js.

Related Posts

11 Comments

  1. “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. 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. 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
    app.use(compression());

    // 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.

  4. 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.
    app.use(compression())

    You aren't compressing requests you are compressing responses.

  5. 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

Leave a Reply

Your email address will not be published. Required fields are marked *