DEV Community

Cover image for How to use compression in Node.js server for better bandwidth ?
Franklin Thaker
Franklin Thaker

Posted on

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

Top comments (0)