How to enable gzip on my node server using connect?

1.5k Views Asked by At

I am trying to have my node server gzip the responses that it gives. I have the following:


const connect = require('connect');
const formDataRequests = ['communities/upsert'];
const app = connect();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
...

I did some research (example https://alligator.io/nodejs/compression/ ) but that requires and express app. Does anyone know what module I can import/etc to get this to gzip the responses?

3

There are 3 best solutions below

3
jcragun On BEST ANSWER

According to the connect docs, you can use the same compression middleware:

const compression = require('compression');
app.use(compression());
0
Asr22 On

Use compression middleware

const compression = require('compression');
app.use(compression());

For serving, node gzip compression file use

app.get('*.js', function (req, res, next) {
    req.url = req.url + '.gz';
    res.set('Content-Encoding', 'gzip');
    res.set('Content-Type', 'text/javascript');
    // console.log('sent')
    next();
});
app.get('*.css', function (req, res, next) {
    req.url = req.url + '.gz';
    res.set('Content-Encoding', 'gzip');
    res.set('Content-Type', 'text/css');
    next();
});

0
HeronAlgoSearch On

Thank you @jcragun . We were able to get it working with:

const compression = require('compression');
app.use(compression());