Compression for JavaScript (Node.js): Complete Guide with Examples
\n\n1. What It Is and Why It's Needed
\nThe 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\nWhy 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\nThe 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.
2. Installation
\nThe 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:
\nnpm install compression\nIf you are using yarn:
\nyarn add compression\nAfter installation, the library will be available for import in your code.
\n\n3. Quick Start — Minimal Working Example
\nThe simplest way to start using compression is to include it as middleware in Express. Below is a minimal server that compresses all responses:
\nconst 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});\nAfter 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.
4. Main Methods/Functions/Classes
\nThe 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\n4.1. compression([options]) — Creating Middleware
\nSignature: compression(options?: CompressionOptions): RequestHandler
What it does: Creates and returns a middleware function for Express. Accepts an optional configuration object.
\nExample:
\nconst 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 Also in library
Rust Application Monitoring: Overview of Tools and Libraries
Event Bus and Observer Pattern in Java: Implementing a Loosely Coupled Architecture
Working with Excel in Go (Golang): Libraries, Examples, and Tools