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)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series