3

I'm trying to serve compressed views with NodeJS/Express.

Even if I configured properly the app, there is no trace of compression. Only static file are compressed.

If I access a view in Chrome, I cannot find the field Content-Encoding: gzip.

The following is configuration of my Express app:

    app.set('port', process.env.PORT || 3000);
    app.set('views', __dirname + '/views');

    app.engine('html', ejs.renderFile);
    app.set('view engine', 'ejs');

    app.use(express.favicon());
    app.use(express.logger('dev'));

    app.use(express.bodyParser());
    app.use(expressValidator());
    app.use(express.methodOverride());

    app.use(express.staticCache());
    app.use(gzippo.staticGzip( path.join(__dirname, 'public') ,{maxAge:86400000} ));
    app.use(gzippo.compress());
    //app.use(express.compress());

    app.use(app.router);

Note that I'm using gzippo for compression. However also the basic compression express.compress() doesn't work.

Giovanni Bitliner
  • 2,032
  • 5
  • 31
  • 50

1 Answers1

5

Move compress to be the first item after view item. Also, be sure to clear cache before checking the Chrome network tag to look for the Encoding header.

Dan Kohn
  • 33,811
  • 9
  • 84
  • 100
  • But **why** must compress be before everything else? – Brainlag Mar 13 '14 at 11:23
  • 1
    I understand the view will have been served before it could be compressed: http://expressjs.com/3x/api.html#app.use "The order of which middleware are "defined" using app.use() is very important, they are invoked sequentially, thus this defines middleware precedence." ...I'm trying to figure out how to use compression in Express 4 myself... – jimmont Mar 28 '14 at 17:16
  • 1
    I was trying to figure out for the life of me why my content-type was not being set. If you clear the cache in Chrome, the content-type is set correctly. Thank you for this. – TYRONEMICHAEL Jan 15 '15 at 15:17
  • 1
    Weird, in the chrome network tab it shows me `Content-Encoding:gzip` but with every gzip test tool it shows me that there is no gzip compression. http://checkgzipcompression.com/ Any ideas which tool I should believe? Ok, googles tool tells me yes: https://developers.google.com/speed/pagespeed/insights/ – Andi Giga Jun 10 '15 at 22:25