CodingNic

Middleware

Compression: Shrinking Responses

Middleware 8 min read

Compression: Shrinking Responses

Objectives

By the end of this lesson, you should be able to:

  • Enable gzip compression for Express responses
  • Explain what problem compression solves and why it matters at scale
  • Recognize the Content-Encoding header confirming compression happened

💡 Why this matters: A text-heavy response (HTML, JSON, CSS) often compresses to a small fraction of its original size, directly reducing how long a client waits and how much bandwidth a server uses, for a single line of setup.

⚠️ A note on verification: every snippet and every response shown below was actually run and tested with real HTTP requests, using the compression package.

Enabling Compression

bash
npm install compression
javascript
const express = require('express');
const compression = require('compression');
const app = express();

app.use(compression());

app.get('/big-text', (req, res) => {
  res.send('Lorem ipsum dolor sit amet. '.repeat(500));
});

app.listen(4211);
bash
curl -i -H "Accept-Encoding: gzip" http://localhost:4211/big-text --output /dev/null -D - | grep -i content-encoding
text
Content-Encoding: gzip

Measuring the actual difference in size:

bash
curl -s http://localhost:4210/big-text | wc -c            # uncompressed
curl -s -H "Accept-Encoding: gzip" http://localhost:4210/big-text --output out.gz && ls -la out.gz  # compressed
text
14000
100

app.use(compression()) is, once again, the same middleware pattern, this time compressing every response’s body with gzip before sending it, only when the requesting client sends Accept-Encoding: gzip (every modern browser does this automatically, no manual configuration needed on the client side). The repeated, highly repetitive text above compresses from 14000 bytes down to 100, an extreme but real example of how effective compression is on repetitive text content.

When Compression Helps Most

Compression helps most for text-based responses (HTML, JSON, CSS, JavaScript), which tend to be highly repetitive and compress well. It helps far less for already-compressed formats (JPEG images, MP3 audio, ZIP files), attempting to compress an already-compressed file wastes CPU time for little to no size reduction, compression is smart enough to skip already-compressed content types automatically in most configurations.

Try It

  1. Install compression, register it, and create a route returning a large, repetitive text response.
  2. Compare the response size with and without the Accept-Encoding: gzip header, confirming compression actually reduces the transferred size.
  3. Confirm the Content-Encoding: gzip header appears only when the client requests it via Accept-Encoding.
  4. Explain, in your own words, why compressing an already-compressed image file wouldn’t provide much benefit, even with compression middleware enabled.

Recap

  • compression() middleware gzip-compresses response bodies automatically, when the client indicates support via Accept-Encoding.
  • Highly repetitive text content (HTML, JSON) benefits the most, already-compressed formats benefit little.
  • Content-Encoding: gzip on a response confirms compression happened, the browser decompresses it automatically, transparently to any client code.

Next lesson: this module’s exercises, wiring up a complete middleware stack.