// Tutorial //

Getting Started with Compression in Node.js

Published on August 28, 2019
Default avatar
By Chris Chu
Developer and author at DigitalOcean.
Getting Started with Compression in Node.js

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Compression in Node.js and Express decreases the downloadable amount of data that’s served to users. Through the use of this compression, we can improve the performance of our Node.js applications as our payload size is reduced drastically.

There are two methods of compression. One is calling it directly in your Node.js app using the compression middleware, and the other is to use it at a reverse proxy level through software like NGINX.

How To Set Up Compression

To start using compression in your Node.js application, you can use the compression middleware in the main file of your Node.js app. This will enable GZIP, which supports different compression schemes. This will make your JSON response and other static file responses smaller.

First, you’ll need to install the npm package for compression:

$ npm i compression --save

Then you can use the module in your application after you initialize your server, like with Express.js:

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

const app = express();

// compress all responses
app.use(compression());

app.get('/', (req, res) => {
  const animal = 'alligator';
  // Send a text/html file back with the word 'alligator' repeated 1000 times
  res.send(animal.repeat(1000));
});

// ...

In the example above, we call a GET operation that will send back a text/html file with the word alligator printed 1000 times. Without compression, the response would come back with a size of around 9kb.

If you turn on compression, the response is sent with a header that states Content-Encoding: gzip, and instead is only 342B.

Options For Compression

In addition to the default setting, you can customize your compression to fit your use case. There are several different properties that you can use in the options object. To get a full list of properties that you can choose, check out the compression documentation.

To add the options for your compression, your code will look a little something like this:

const shouldCompress = (req, res) => {
  if (req.headers['x-no-compression']) {
    // don't compress responses if this request header is present
    return false;
  }

  // fallback to standard compression
  return compression.filter(req, res);
};

app.use(compression({
  // filter decides if the response should be compressed or not, 
  // based on the `shouldCompress` function above
  filter: shouldCompress,
  // threshold is the byte threshold for the response body size
  // before compression is considered, the default is 1kb
  threshold: 0
}));

And there you have it! Make sure you use compression for your Node.js app to keep your payload sizes small and snappy!


Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up
About the authors
Default avatar
Chris Chu

author

Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?
Leave a comment

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!