Compression middleware makes your web app send smaller files to users. This helps pages load faster and saves internet data.
Compression middleware in Node.js
import compression from 'compression'; import express from 'express'; const app = express(); app.use(compression());
app.use(compression()) before your routes to compress all responses.compression() to control how compression works.app.use(compression());
app.use(compression({ threshold: 0 }));app.use(compression({ filter: (req, res) => {
return req.headers['x-no-compression'] ? false : compression.filter(req, res);
}}));This simple Express server uses compression middleware. When you visit the homepage, it sends a large repeated text. Compression makes this response smaller and faster to load.
import express from 'express'; import compression from 'compression'; const app = express(); // Use compression middleware app.use(compression()); app.get('/', (req, res) => { const bigText = 'Hello! '.repeat(1000); // Large response res.send(bigText); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
Compression works best on text-based responses like HTML, CSS, JavaScript, and JSON.
Binary files like images or videos usually should not be compressed again by middleware.
Use browser DevTools Network tab to see if responses are compressed (look for content-encoding: gzip header).
Compression middleware makes server responses smaller to speed up loading.
It is easy to add with app.use(compression()) in Express apps.
Check your app's responses in browser tools to confirm compression is working.
Practice
compression middleware in a Node.js Express app?Solution
Step 1: Understand what compression middleware does
Compression middleware compresses the data sent from the server to the client to reduce its size.Step 2: Identify the benefit of compression
Smaller response sizes mean faster loading times for users because less data travels over the network.Final Answer:
To reduce the size of server responses and speed up loading times -> Option DQuick Check:
Compression = smaller responses = faster loading [OK]
- Confusing compression with security or logging
- Thinking compression parses request data
- Assuming compression adds headers unrelated to size
Solution
Step 1: Recall the syntax for middleware usage in Express
Middleware functions are passed as functions, so you must callcompression()to get the middleware function.Step 2: Identify the correct syntax
app.use(compression());correctly calls the compression function and passes the middleware to Express.Final Answer:
app.use(compression()); -> Option CQuick Check:
Middleware needs function call syntax () [OK]
- Forgetting parentheses after compression
- Passing string instead of function
- Using wrong function name like compress
import express from 'express';
import compression from 'compression';
const app = express();
app.use(compression());
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(3000);Solution
Step 1: Understand compression middleware behavior
Compression middleware compresses responses only if the client sends an Accept-Encoding header that supports compression (like gzip).Step 2: Analyze the code snippet
The middleware is applied globally withapp.use(compression()), so all responses, including GET '/', will be compressed if the client supports it.Final Answer:
The response will be compressed if the client supports it -> Option AQuick Check:
Compression depends on client support [OK]
- Assuming compression always compresses regardless of client
- Thinking compression only works on POST
- Believing compression requires extra config to avoid errors
const express = require('express');
const compression = require('compression');
const app = express();
app.use(compression);
app.get('/', (req, res) => {
res.send('Hello');
});
app.listen(3000);Solution
Step 1: Check how compression middleware is added
The code usesapp.use(compression);which passes the compression function itself, not the middleware returned by calling it.Step 2: Identify correct usage
Compression middleware must be called ascompression()to return the middleware function, so it should beapp.use(compression());.Final Answer:
Compression middleware is not called as a function -> Option BQuick Check:
Middleware must be called with () [OK]
- Passing middleware function without calling it
- Thinking middleware order doesn't matter here
- Confusing route handler requirements with middleware
Solution
Step 1: Recall compression middleware options
The official compression middleware option to set minimum response size for compression isthreshold, which accepts bytes.Step 2: Match the correct option name and value
Settingthreshold: 1024means compress responses only if size is greater than 1024 bytes (1KB).Final Answer:
app.use(compression({ threshold: 1024 })); -> Option AQuick Check:
Use threshold option to set minimum size [OK]
- Using incorrect option names like minSize or sizeLimit
- Not specifying size in bytes
- Assuming compression applies to all sizes by default
Ready to prove it?
Pass the test and this pattern moves unknown
