Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express.js compression module not working

I'm trying to gzip static files in my express application to reduce the size of scripts. I tried using express' "compression" module, but it won't compress anything.

I looked online for people having the same problem, but all the answers I found were only about putting app.use(compression()) above all routes and setting compression's size threshold to zero, I did both things but I'm still getting nowhere.

Here's the relevant code:

var express = require("express");
var fs = require('fs');
var xml2js = require("xml2js");
var auth = require("./authenticator.js");
var bodyParser = require('body-parser');
var _ = require('lodash');
var compression = require('compression')

var app=express();

app.use(compression({threshold : 0}));

var jsonParser = bodyParser.json();

var urlencodedParser = bodyParser.urlencoded({
    extended: true
}); 

app.get('/', function(req, res){
    res.header('X-UA-Compatible', 'IE=Edge');
    res.setHeader("Cache-Control", "public, max-age=604800, must-revalidate");
    sendFile(res, 'index.html', 'text/html');
});

app.get('/testGZIP', function(req, res){
    res.setHeader('Content-Type', 'application/json');
    res.setHeader("Cache-Control", "public, max-age=604800, must-revalidate");
    var msg = JSON.stringify({'text':_.times(5000, _.uniqueId())});
    res.write(msg);
    res.end();
});

app.use(function(req, res, next) {
    var urls = ['modules/', 'font/', 'js/', 'css/', 'views/', 'img/'];
    if(_.some(urls, (el) => _.includes(req.url, el))) {
        console.log("g-zipping request " + req.url + "...");
        res.setHeader("Cache-Control", "public, max-age=604800, must-revalidate");
    }
    return next();
});
app.use('/font/', express.static('./font/'));
app.use('/modules/', express.static('./node_modules/'));
app.use('/css/', express.static('./css/'));
app.use('/views/', express.static('./views/'));
app.use('/img/', express.static('./img/'));
app.use('/js/', express.static('./js/'));

var server = app.listen(8010, function(){
   var host = server.address().address;
   var port = server.address().port;

   console.log("\nServer started on ip " + host + ' on port ' +port + " - " + (new Date()).getHours() + ":" + (new Date()).getMinutes() + "\n");
});

Here are the headers for /testGZIP: (any other static resource returns the same)

enter image description here

The content, of course, is not compressed.

What am I doing wrong?


EDIT:

I tried doing this with zlib rather than gzip, but isn't compressing either:

app.get('/testZlib', function(req,res){
    res.writeHead(200, {
    'Content-Encoding': 'gzip' });
    fs.createReadStream('js/client-main.min.js').pipe(zlib.createGzip()).pipe(res);
});
like image 359
Hankrecords Avatar asked Mar 17 '26 11:03

Hankrecords


1 Answers

I think you need to set content encoding in you case to get it work.

ex:- res.set('Content-Encoding', 'gzip');

like image 74
RickyN73 Avatar answered Mar 20 '26 07:03

RickyN73



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!