Connect 2.0 添加了对基于 new zlib stuff 的 compress() 中间件的支持,该中间件刚刚出现在 Node Core API 中。
您可以通过在 package.json 文件中添加连接 2.0 的依赖项来在您的 express 服务器中使用它:
{
...
dependencies: {
"connect" : "2.x",
"express" : "2.x",
// etc..
}
}
然后将以下逻辑应用到您的快速应用配置中:
// 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);
请注意,这些东西还是很新,虽然我可以让它在本地工作,但我的 Heroku 云应用程序抱怨在预提交挂钩期间对 Compress 2.x 的依赖通过 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-'
如您所见,他们仍在使用旧版本的节点 (0.4.7)。
更新:
实际上,我可以通过在package.json 中添加相应的engines 部分来让Heroku 部署它:
{
...
"engines": {
"node": ">= 0.6.0 < 0.7.0"
}
}
这些是使用 http 压缩测试器时的结果:
2014 年 6 月更新
嗨,如果你现在正在阅读这篇文章。不要忘记上面的内容只与 Express 2.0 相关。
Express 3.0 和 4.0 使用不同的语法来启用 http 压缩,请参阅下面 gasolin 的帖子。