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-Encodingheader 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
compressionpackage.
Enabling Compression
npm install compression
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);
curl -i -H "Accept-Encoding: gzip" http://localhost:4211/big-text --output /dev/null -D - | grep -i content-encoding
Content-Encoding: gzip
Measuring the actual difference in size:
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
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
- Install
compression, register it, and create a route returning a large, repetitive text response. - Compare the response size with and without the
Accept-Encoding: gzipheader, confirming compression actually reduces the transferred size. - Confirm the
Content-Encoding: gzipheader appears only when the client requests it viaAccept-Encoding. - Explain, in your own words, why compressing an already-compressed image file wouldn’t provide much benefit, even with
compressionmiddleware enabled.
Recap
compression()middleware gzip-compresses response bodies automatically, when the client indicates support viaAccept-Encoding.- Highly repetitive text content (HTML, JSON) benefits the most, already-compressed formats benefit little.
Content-Encoding: gzipon 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.