How to use compression in Node.js server for better bandwidth ?

franklinthaker

Franklin Thaker

Posted on May 15, 2024

How to use compression in Node.js server for better bandwidth ?
const express = require("express");
const compression = require("compression");
const app = express();

app.set("etag", false);
app.use(compression());

app.get("/data", (req, res) => {
  return res.json({
    message: "Hello, Axel Blaze, This is a test message. ".repeat(10000),
  });
});

app.listen(3000, function () {
  console.log("listening on 3000");
});

Enter fullscreen mode Exit fullscreen mode

How to start your server & check if gzip compression is working or not!

DEBUG=compression node app.js

Tip

Make sure you pass correct Request header i.e. Accept-Encoding: gzip

Output examples:

With Compression
With Compression

Without Compression
Without Compression

💖 💪 🙅 🚩
franklinthaker
Franklin Thaker

Posted on May 15, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related