How to use compression in Node.js server for better bandwidth ?
Franklin Thaker
Posted on May 15, 2024
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");
});
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:
💖 💪 🙅 🚩
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.