How to disable response data compression in Express?

Viewed 205

I want to receive in response from the server 'Content-Length' For this i want to disable gzip compression. I use for this comprassion middleware:

const shouldCompress = (req, res) => {
    if(req.headers['x-no-comprassion']) {
        return false
    } 
}

app.use(compression({threshold: 5, filter: shouldCompress}))

And also send 'x-no-comprassion' header from client, but it still doesn't work. What could be the problem?

2 Answers

I've tested with this code:

const compression = require('compression');
const express = require('express');
const app = express();

function shouldCompress(req, res) {
  if (req.headers['x-no-compression']) return false;
  return compression.filter(req, res);
}

app.use(compression({threshold: 5, filter: shouldCompress}));
app.get('/', function (req, res) { res.json({state: "ok"}); });
app.listen(8888, () => { console.log(`Started http://localhost:8888`) });

When I launch curl localhost:8888 -v
=> I have: Content-Length: 14

When I launch curl localhost:8888 -v -H 'Accept-Encoding: gzip, deflate'
=> No content-length in response, and a binary output Warning: Binary output can mess up your terminal.

When I launch curl localhost:8888 -v -H 'Accept-Encoding: gzip, deflate' -H 'x-no-compression: 1'
=> I have: Content-Length: 14

So, there is no issue, it works as excepted. How did you send the header? Did you set a value?

Related