QA
Intermediate LevelExpress JS

How do you enable response compression in Express using the compression middleware?

❓ Interview Prompt: Explain response compression (Gzip/Brotli), compression middleware configuration, threshold options, and offloading to reverse proxies.

The 'compression' middleware compresses outgoing HTTP response body payloads (HTML, CSS, JSON) using Gzip/Brotli algorithms to reduce network transfer sizes.

Usage & Production Note:

Configured via 'app.use(compression({ threshold: 1024 }))' (compresses payloads larger than 1KB).

Production Best Practice: In high-scale production, offload SSL and compression to NGINX, Cloudflare, or AWS CloudFront to preserve Node.js CPU cycles!

Code Implementation Example

javascript
import express from 'express';
import compression from 'compression';

const app = express();

// Compress payloads larger than 1KB
app.use(compression({ threshold: 1024 }));

app.get('/api/large-data', (req, res) => {
  res.json({ data: Array(5000).fill('sample text payload') });
});
💡Enabling response payload Gzip compression middleware.
🎯 Interview Key Takeaway

Use the compression middleware to Gzip response payloads >1KB. Offload compression to NGINX/Cloudflare in high-traffic production.

#compression#Gzip#Performance#Intermediate
Back to all Express JS Questions→

More Express JS Interview Questions

Beginner LevelExpress JS

What is Express.js and how does the middleware pipeline work?

Beginner LevelExpress JS

What is the difference between req.params, req.query, and req.body?

Beginner LevelExpress JS

How does express.Router() work for building modular route controllers?