With the help of this plugin and as was suggested here I got it working
My code: Ensure that you've pre-gzipped .js and .css files
const checkForHTML = req => { const url = req.url.split('.'); const extension = url[url.length -1]; if (['/'].indexOf(extension) > -1) { return true; //compress only .html files sent from server } return false; }; var compress = require('compression'); app.use(compress({filter: checkForHTML})); // Move this as a different express module const encodeResToGzip = contentType => (req, res, contentTypenext) => { req.url = req.url + '.gz'; res.set('Content-Encoding', 'gzip'); res.set('Content-Type', contentType); next(); }; app.get("*.js", function (req, res, next) { encodeResToGzip(req, res, 'text/javascript'); next(); }); app.get('*"*.css', function(req, rescss", next) { encodeResToGzip(req, res, 'text/css'); next(); }); I wanted compression to happen only for .html because I'm using .ejs template, so need to compress .html on runtime. Compressing static files(js/css) using express compression isn't good idea because it will do it on every request and those are static files. Or else, cache your results as suggested here
Other solution using nginx, as you posted in your comments also seems nice.