• Home
  • Q&A
  • Express gzip static content

Express gzip static content

June 16, 2011
12 score
5 answers

Express and connect appeared to have removed their gzip functions because they were too inefficient. Are there any reliable solutions to gzip with express-js currently?


15 score

Connect 2.0 has added support for compress() middleware based on the new zlib stuff with that has just come out in Node Core API.

You can make use of this in your express server by adding a dependency to connect 2.0 in your package.json file:

    dependencies: {
        "connect" : "2.x",
        "express" : "2.x",
        // etc..

And then apply the following logic into your express app configuration:

// Create static file server with gzip support
var app = express.createServer(express.logger());
app.use(express.static(__dirname + '/public'));

Please note that this stuff is still pretty new and while I could get it to work locally, my Heroku cloud application complained about the dependency on Compress 2.x during the pre-commit hook when deploying via git:

-----> Heroku receiving push
-----> Node.js app detected
-----> Resolving engine versions
       Using Node.js version: 0.4.7
       Using npm version: 1.0.106
-----> Fetching Node.js binaries
-----> Vendoring node into slug
-----> Installing dependencies with npm
       npm ERR! Error: No compatible version found: connect@'>=2.0.0- <3.0.0-'

As you can see, they're still using an old version of node (0.4.7).


Actually, I could get Heroku to deploy this by adding the corresponding engines section in the package.json:

    "engines": {
        "node": ">= 0.6.0 < 0.7.0"

And these are the results when using a http compression tester:

enter image description here

answered March 17, 2012
7 score

Express 3.0 now has compress() support:

var app = express();
// gzip
// static
app.use("/public", express.static(__dirname+'/public'));
// listen
answered March 10, 2013
5 score

I have also searched npm and found for example:

Gzippo has recently been developed(2 days ago) which I think is a good thing. I can't tell you about production usage. You should test/benchmark it yourself. I would also probably use a CDN for a live site or Nginx to host my static files instead of some nodejs module.

answered June 16, 2011
3 score

Connect will support the new zlib stuff in Node in the next release

answered October 12, 2011
1 score

If you've searched the npm you may have come across node-compress.

It shouldn't be too hard to inject it as middleware into express.

answered June 16, 2011