Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Express.js · Module

compression

Express’s compression middleware compresses eligible HTTP response bodies using deflate, gzip, or Brotli. Install the compression package and mount compression([options]) with app.use().

How it works

Calling compression with an options object creates middleware that evaluates each traversing response for compression. The default filter checks whether the response content type is compressible, while options can adjust filtering, compression level, threshold, and zlib or Brotli behavior.

Syntax

compression([options])

Parameters

NameTypeDescription
optionsobjectOptional configuration object controlling filtering, compression level, threshold, and zlib or Brotli behavior.

Returns

Middleware function that attempts to compress response bodies for requests passing through it.

Key points

  • Supported response codings include deflate, gzip, and Brotli, represented by br.
  • Invoke compression with an optional options object to obtain middleware for app.use.
  • The filter option lets application code decide whether an individual response should be considered.
  • The threshold option controls the response size at which compression is considered.
  • The options object accepts additional zlib and Brotli configuration supported by the underlying algorithms.

Common mistakes

  • Responses marked with Cache-Control no-transform are intentionally left uncompressed.
  • Mount the middleware early enough in the stack for intended responses to pass through it.
  • Increasing compression level trades additional CPU time for smaller output.

Example: Enable response compression

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

const app = express();
app.use(compression());
app.get('/', (req, res) => {
  res.type('text').send('A response that may be compressed.');
});
app.listen(3000);

Installs compression middleware for requests reaching the application, allowing supported response bodies to be compressed according to the client and middleware options.

When to use it

Improve response transfer efficiency

Reduce transfer size for text-heavy API or web responses.

Customize compression policy

Apply compression selectively based on request or response properties.

Adjust compression behavior

Tune output size and processing cost for application workloads.

Interview questions

QWhat does compression([options]) return and do?

compression([options]) returns middleware. That middleware considers response bodies for requests that pass through it, using the supplied options and the client’s supported coding.

Follow-up: How can compression be selectively disabled? Provide a filter function that returns false for requests or responses that should not be compressed.

QHow does the default compression filter decide?

The default filter uses the compressible module to inspect res.getHeader('Content-Type'). A response is considered for compression when that content type is compressible.

Follow-up: What does a custom filter need to return? It must return true to consider compression or false to skip it.

QWhich compression codings and option families are supported?

The middleware supports deflate, gzip, and Brotli through the br coding. Its options can also include zlib options and Brotli options.

Follow-up: What is the trade-off of raising the zlib compression level? It can improve compression while increasing completion time.

  • express()
  • app.use()
  • compressible
  • zlib
  • Cache-Control

Source: compression middleware - Express.js. Checked 2026-10-15.