Compression for JavaScript (Node.js): Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Compression for JavaScript (Node.js): Complete Guide with Examples

\n\n

1. What It Is and Why It's Needed

\n

The compression library for Node.js is a middleware for the Express framework (and other compatible frameworks) that automatically compresses HTTP server responses using Gzip, Deflate, or Brotli algorithms. It is the de facto standard for optimizing web application performance on Node.js, as it significantly reduces the size of transmitted data (HTML, CSS, JavaScript, JSON, images, etc.) between the server and the client.

\n\n

Why is this needed? In the modern web, page load speed is critically important for user experience and SEO. Data compression can reduce traffic volume by 3–10 times (depending on the content type). For example, an HTML file of 100 KB after Gzip compression can weigh only 10–15 KB. This is especially relevant for mobile users with limited traffic and for APIs returning large JSON objects. Without using compression, the server will send "raw" data, which increases load time and network load.

\n\n

The library fully automates the process: it checks the Accept-Encoding header in the client request, selects the appropriate compression algorithm (usually gzip or brotli, if supported), compresses the response, and adds the corresponding headers (Content-Encoding, Vary). The developer only needs to include the middleware in the Express request processing chain. This makes compression an indispensable tool for any production Node.js application.

\n\n

2. Installation

\n

The library is installed via npm (Node Package Manager). Make sure you have Node.js installed (version 12 or higher). Run the command in the root of your project:

\n
npm install compression
\n

If you are using yarn:

\n
yarn add compression
\n

After installation, the library will be available for import in your code.

\n\n

3. Quick Start — Minimal Working Example

\n

The simplest way to start using compression is to include it as middleware in Express. Below is a minimal server that compresses all responses:

\n
const express = require('express');\nconst compression = require('compression');\nconst app = express();\nconst port = 3000;\n\n// Include compression middleware\napp.use(compression());\n\n// Regular route\napp.get('/', (req, res) => {\n  // Send a large response (repeat the string 1000 times)\n  const bigData = 'Hello, World! '.repeat(1000);\n  res.send(bigData);\n});\n\napp.listen(port, () => {\n  console.log(`Server running at http://localhost:${port}`);\n});
\n

After starting (node app.js), open your browser at http://localhost:3000. In the developer tools (Network tab), you will see that the response is compressed (header Content-Encoding: gzip). The response size will be several times smaller than the original.

\n\n

4. Main Methods/Functions/Classes

\n

The compression library provides one main function — a middleware that is configured through an options object. Let's look at 8 key aspects of working with it.

\n\n

4.1. compression([options]) — Creating Middleware

\n

Signature: compression(options?: CompressionOptions): RequestHandler

\n

What it does: Creates and returns a middleware function for Express. Accepts an optional configuration object.

\n

Example:

\n
const compression = require('compression');\nconst app = require('express')();\n\n// Without options (default behavior)\napp.use(compression());\n\n// With options\napp.use(compression({\n  level: 6,          // Compression level (0-9)\n  threshold: 1024,   // Do not compress responses smaller than 1 KB\n  filter                    

Recommendations