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?
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
}));