2

I use compression library to compress all my express responses. Unfortunately, it appears that none of the reponses are compressed. Yet, I specify an accept-encoding: "gzip" in the client call header. Here is the code:

front:

export const api = ax.create({
  baseURL: "http://localhost:8000",
  timeout: 1000,
  withCredentials: true,
  headers: {
    'Accept-Encoding': 'gzip',
  }
});

const contact = () => {
    api.get("/contact")
    .then(res=> console.log("contact response", res))
    .catch(err=> console.log("contact err", err))
  }

back:

const app = require("express")();
const cors = require("cors");
const compression = require("compression")

app.use(cors({ credentials: true, origin: ["http://localhost:3000"] }));
app.use(compression());


app.get('/contact',  (req, res) => {
    res.status(200).json({hello: "world"})
  })

In chrome network tab, I don't have the content-encoding: gzip. How to fix this?

DoneDeal0
  • 5,273
  • 13
  • 55
  • 114
  • Make sure your anti-virus isn't using an HTTP Scanner More info: https://stackoverflow.com/questions/58391816/x-content-encoding-over-network-in-response-header-but-not-content-encoding – lbragile May 18 '22 at 04:18
  • See my answer here: https://stackoverflow.com/a/75308588/10030693 – Gilbert Feb 01 '23 at 10:13
  • Thanks Gilbert, but this question has already been answered in March 24, 2021 at 16:38. We are now February, 1st, 2023. – DoneDeal0 Feb 01 '23 at 11:24

1 Answers1

8

The default threshold for compression is 1kb (Source). If your request is under 1kb, it won't be compressed.

You can modify the threshold and set it to 0 bytes, if you want to.

app.use(compression({
  threshold: 0
}));
Keimeno
  • 2,512
  • 1
  • 13
  • 34