Express gzip static content

34,823

Solution 1

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(connect.compress());
app.use(express.static(__dirname + '/public'));
app.listen(80);

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).


UPDATE:

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

UPDATE June 2014

Hiya, if you are reading this now. Dont forget that the stuff above is only relevant to Express 2.0.

Express 3.0 and 4.0 use different syntax for enabling http compression, see post by gasolin just below.

Solution 2

Express 3.0 now has compress() support:

var app = express();
// gzip
app.use(express.compress());
// static
app.use("/public", express.static(__dirname + '/public'));
// listen
app.listen(80);

EDIT for Express 4.0, compress become the separate middleware. So you have to install and import to use it:

var compress = require('compression');
app.use(compress()); 

Solution 3

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.

Solution 4

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

Solution 5

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.

Share:
34,823
HyderA
Author by

HyderA

Updated on January 18, 2020

Comments

  • HyderA
    HyderA over 4 years

    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?

  • HyderA
    HyderA almost 13 years
    There are plenty of compression libraries available, but I want to know if people are using them in production successfully.
  • Raynos
    Raynos almost 13 years
    @gAMBOOKa I'm using that one personally but not for production just for development.
  • serby
    serby almost 13 years
    We are using gzippo in production for a large UK newspaper. The beauty of configuring the content compression in the node layer is that you can then simply put Varnish, Squid or a CDN upstream and it will obey the http headers and keep a cached zipped copy on the proxy/CDN for all subsequent request. This keeps the cache config with your node code and in your git/svn etc.
  • josh3736
    josh3736 almost 12 years
    @AndreyLushnikov: You have to use() compress before static. compress replaces res.write and res.end so that it can proxy data through zlib. If static is before compress, it handles the entire request before the compress middleware has a chance to run and patch the res methods. Steven, I've updated your answer to swap the use() calls.
  • HyderA
    HyderA about 11 years
    @gasolin states below that Express 3.0 now has compress() support. If you're reading this, could you please update your answer?
  • jlmakes
    jlmakes about 11 years
    How did you do the http compression tester?
  • Ray Shan
    Ray Shan about 10 years
    Express 4.x removed almost all middleware, including compress. You can use Connect's compression middleware now: github.com/expressjs/compression.
  • smg
    smg over 9 years
    The above provided link for the compression tester doesn't seem to be working anymore. Here is an alternative: gidnetwork.com/tools/gzip-test.php
  • Muhammad Umer
    Muhammad Umer about 9 years
    does it require extra steps in heroku
  • CMCDragonkai
    CMCDragonkai about 9 years
    In what circumstances does the compression trigger? I just added it but wrote res.send and no gzipped content came out. Does it require res.end or some other trigger?
  • Spawnrider
    Spawnrider almost 8 years
    Don't forget to add { threshold: 0 } when you want to compress static files with Express 4.x
  • master_dodo
    master_dodo almost 7 years
    Apart from run time template->html, is it good to use compression for .js and .css if we can compress them? Because it won't be good idea to use for SSR where bcoz of heavy traffic, it would have to compress for each file requested.. Can you please tell how to use compress just for .html? I'm using .ejs for templates.. @Spawnrider Also, I didn't add threshold: 0 for my .css or .js files, it worked right away