16

I don't want to use a library which gzips on the fly, because of the overhead.

The website has some dynamic components which is implemented in node.js.

I have some static js and css files as well as their gzipped counterparts. I want to serve the gzipped version only to browsers which support it.

I considered using the static middleware in express to serve the static files, along with some URL rewriting middleware to conditionally serve the gzipped files. However, I cannot find any conditional rewrite module.

I cannot believe that no one has done this, or that it needs so many work arounds. What am I missing?

On a different note, is serving up static files via node.js too expensive? On the other hand, using Apache for static files and running node.js behind seems bad as well. What is the least stupid configuration for AWS EC2 hosting?

2 Answers 2

17

This code is working with Express 4

app.get('*.js', function(req, res, next) {
  req.url = req.url + '.gz';
  res.set('Content-Encoding', 'gzip');
  res.set('Content-Type', 'text/javascript');
  next();
});
app.get('*.css', function(req, res, next) {
  req.url = req.url + '.gz';
  res.set('Content-Encoding', 'gzip');
  res.set('Content-Type', 'text/css');
  next();
});

Make sure you are setting the right content type - mime-types reference - for the compressed file like this:

res.set('Content-Type', 'text/css');

or it will not work

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

2 Comments

I was doing the same but to res.originalUrl += '.gz' Any idea why it doesn't work?
shouldn't there be a conditional first - some kind of check that the browser will accept the gz version?
16

If you want to have best performance for your clients, just use a CDN. It will take care of gzipping for you and a lot of other stuff. If you need help you can use express-cdn module.

If you don't like CDNs for some reasons, your best bet is using nginx. I see it tagged in your question, but you didn't mention anything about it. nginx is way faster than node.js. For nginx you can check it's gzip-static module.

If you still want to use node.js, then connect-gzip-static is your best bet. It works almost same as nginx-gzip-static module. If there's a .gz file counterpart of the requested file, then it will use that, else it will fallback to normal connect-static module.

Don't forget to compile files beforehand, if you are using gulp, then you might also use gulp-gzip. If not just use gzip command.

There's also gzippo, it gzips on the fly, but it also caches the result in the memory. So it will be only gzipped once.

4 Comments

Thank you very much for the response. I did not know about connect-gzip-static and express-cdn. So this is very helpful. I need to use node.js because there is also a dynamic API component to the website. So only a CDN is not sufficient. It seems from a brief reading that express-cdn only helps to manage resources stored in S3, and not suitable for a website with dynamic components. I think I will try to go with Nginx, and hopefully the features it provides is sufficient to replace the functionality I currently have in the htaccess.
Well you put the static resources, in the CDN, not the dynamic ones. I think anything that's not generated on the fly(when the request hits the server) and not tied to a particular request, should be considered as static, and you should be able to host it on a CDN. On deployment, you would just upload all static resources to the CDN and they'll be served from there. Anything you can serve statically with nginx, should work with a CDN I believe.
I don't see a way to host the static part on a CDN, but only have the dynamic part go to a NGINX server. Is such a setup possible? It seems to me NGINX is the only option, since we need something to decide which queries are for static resources, and which are for dynamic. Please let me know if I am missing something. Thanks again for all the help!
Use a separate domain, for static stuff and put them on a CDN.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.