Hi! My name is Mark Stuart

My thoughts and other ramblings

Pro Tip: Enable GZip Compression in Express With One Line of Code

As you might already know, GZip is one of the easiest wins you can make to improve the performance of your website. Generally, GZipping will reduce the size of your HTTP responses by 70%.

If you’re using Express, it doesn’t GZip static assets out-of-the-box so you’ll be missing out on the many benefits of GZip.

Here’s how you enable it:

  1. Add this lines to your server.js (or equivalent file where you define middleware and startup your server)
1
2
3
4
app.configure(function() {
  app.use(express.compress());
  app.use(express.static(__dirname + "/public", { redirect: false }));
});

express.compress() must be before express.static(). This is where the magic happens :)

Restart your server, and voila… GZip!

Inspect the HTTP response headers and you should see Content-Encoding: gzip

Image showing that GZip is enabled