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?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- Keeping track of variable instances
Express 3.0 now has compress() support:
EDIT for Express 4.0, compress become the separate middleware. So you have to install and import to use it:
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.
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:And then apply the following logic into your express app configuration:
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:
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 thepackage.json
:And these are the results when using a http compression tester:
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.
Connect will support the new zlib stuff in Node in the next release
I have also searched npm and found for example:
https://github.com/tomgallacher/gzippo
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.