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.

2
  • why are you using gzippo? it doesn't support node 0.10 Commented Nov 23, 2013 at 5:56
  • See my answer here: stackoverflow.com/a/75308588/10030693 Commented Feb 1, 2023 at 10:14

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

But why must compress be before everything else?
I understand the view will have been served before it could be compressed: 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...
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.
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. checkgzipcompression.com Any ideas which tool I should believe? Ok, googles tool tells me yes: developers.google.com/speed/pagespeed/insights

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.