Trying to get express.compress() working

2,797 views
Skip to first unread message

bill

unread,
Apr 18, 2012, 4:40:58 PM4/18/12
to Express
I'm trying to figure out how to use the staticCache(), static(), and
compress() middlewares with Express 3.0 (not specific to 3.0, never
had it working in 2.5 either). This is what my app configuration
looks like:

app.configure(function() {
app.use(express.favicon(__dirname + '/../public/favicon.ico',
{maxAge: 8640000000}));
app.use(express.bodyParser());
app.use(express.cookieParser('foo'));
app.set('views', __dirname + '/views');
app.engine('.html', mustache({cache: true}).render);
app.use(express.session(
{ store: sessionStore,
secret: 'foo'
})
);
app.use(express.staticCache());
app.use(express.static(__dirname + '/../public', {maxAge:
8640000000}));
app.use(express.compress());
});

When I look the responses, the staticCache() always misses and my .css
and .js files are not being sent compressed. I figure the ordering is
wrong or I'm missing a setting but I can't figure it out. My routes
are defined after the app.configure() block.

Without the express.compress() line, the staticCache() starts
reporting cache hits again.

Does somebody have a sample app that uses staticCache, static, and
compress and has everything working?



Julian Knight

unread,
Apr 24, 2012, 11:39:22 AM4/24/12
to expre...@googlegroups.com
I've not yet tried the caching with Express 3 but I am certainly getting the same issue with compression. 
My dynamic pages have the response header:
  1. Connection:
    keep-alive
  2. Content-Encoding:
    gzip
  3. Content-Type:
    text/html; charset=utf-8
  4. Transfer-Encoding:
    chunked
  5. Vary:
    Accept-Encoding
  6. X-Powered-By:
    Express
  7. X-Response-time:
    36ms
But the CSS has the response:
  1. Accept-Ranges:
    bytes
  2. Cache-Control:
    public, max-age=0
  3. Connection:
    keep-alive
  4. Date:
    Tue, 24 Apr 2012 15:19:26 GMT
  5. Last-Modified:
    Wed, 18 Apr 2012 19:49:33 GMT
  6. Vary:
    Accept-Encoding
  7. X-Powered-By:
    Express
  8. X-Response-time:
    1ms

David

unread,
Apr 24, 2012, 8:26:09 PM4/24/12
to expre...@googlegroups.com
As a follow up about the staticCache() my static css file is cached once you changed the order:
   app.use(express.compress());  
   app.use(express.staticCache()); 
   app.use(express.static(__dirname + '/public'));


--
You received this message because you are subscribed to the Google Groups "Express" group.
To view this discussion on the web visit https://groups.google.com/d/msg/express-js/-/Dof5p_v2dCIJ.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.

David

unread,
Apr 24, 2012, 8:11:32 PM4/24/12
to expre...@googlegroups.com
app.use(express.compress()) must come before the express.static() middleware.  Some middleware needs to come before others.   compress() is simply modifying the response and not actually serving.  When you put the static middleware before compress it never ends up calling the compress middleware for static files because the static middleware "served" it already.

You also need to be aware that if the browser already has the static file in it's cache it will not resend it so the response header will not indicate it was compressed.  Clear or disable your browser cache when testing that the css file is being compressed after you reorder the middleware.

I will check the staticCache issue later but try without it to verify compression works.

David

“For a successful technology, reality must take precedence over public relations, for nature cannot be fooled.” - Richard Feynman

On Apr 24, 2012, at 10:39 AM, Julian Knight wrote:

Reply all
Reply to author
Forward
0 new messages