0
0
NodejsHow-ToBeginner · 3 min read

How to Use Compression in Node.js for Faster Responses

In Node.js, you can use the compression middleware to automatically compress HTTP responses, making them smaller and faster to transfer. Simply install the compression package and add it as middleware in your Express app to enable gzip or Brotli compression.
📐

Syntax

The compression middleware is used by requiring it and then calling it as a function to get the middleware handler. You add it to your Express app using app.use(). It compresses response bodies for all requests that pass through it.

  • const compression = require('compression'): imports the compression module.
  • app.use(compression()): adds compression middleware to your app.
javascript
const compression = require('compression');
const express = require('express');
const app = express();

app.use(compression());

app.get('/', (req, res) => {
  res.send('Hello, compressed world!');
});

app.listen(3000);
💻

Example

This example shows a simple Express server using the compression middleware. It compresses the response for the root URL, reducing the size of the text sent to the browser.

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

// Enable compression for all responses
app.use(compression());

app.get('/', (req, res) => {
  // Send a large string to see compression effect
  const bigText = 'Hello, compressed world! '.repeat(1000);
  res.send(bigText);
});

app.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});
Output
Server running on http://localhost:3000
⚠️

Common Pitfalls

Some common mistakes when using compression in Node.js include:

  • Not adding compression() before routes, so responses are not compressed.
  • Compressing already compressed files like images or videos, which wastes CPU.
  • Forgetting to handle errors or not testing if the client supports compression.

Always place app.use(compression()) near the top of your middleware stack.

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

// Wrong: compression middleware added after routes
app.get('/', (req, res) => {
  res.send('No compression here');
});

app.use(compression()); // This won't compress the above route

// Correct way:
// app.use(compression());
// app.get('/', ...);
📊

Quick Reference

Key points to remember when using compression in Node.js:

  • Install with npm install compression.
  • Require and use it early in your Express app.
  • It automatically chooses the best compression method supported by the client.
  • Do not compress static files; serve them with a static middleware or CDN.

Key Takeaways

Use the compression middleware early in your Express app to compress HTTP responses.
Compression reduces response size, speeding up data transfer to clients.
Avoid compressing already compressed files like images to save CPU resources.
Test your server to ensure clients receive compressed responses correctly.
Install compression with npm and add it using app.use(compression()).