快速gzip静态内容 [英] Express gzip static content

查看:131
本文介绍了快速gzip静态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Express和connect似乎已经删除了它们的gzip函数,因为它们效率太低。有没有任何可靠的解决方案gzip与express-js目前?

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?

推荐答案

Connect 2.0 增加了对 compress( )中间件,基于新的zlib东西,它刚刚出现在Node Core API。

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.

您可以在快速服务器中使用此功能,方法是在您的 package.json file:

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

请注意,这些东西仍然是新的,而我可以它在本地工作,我的 Heroku 云应用程序在预提交钩子期间抱怨Compress 2.x的依赖通过git部署时:

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-'

如您所见,他们仍然使用旧版本的节点(0.4.7)。

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

更新:

其实我可以通过添加相应的引擎来获得Heroku来部署> package.json 中的部分:

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"
    }
}

这些是使用http压缩测试器时的结果:

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

< img src =https://i.stack.imgur.com/f8w8D.pngalt =enter image description here>

2014年6月更新

如果你现在阅读这个。不要忘记上面的内容只与Express 2.0有关。

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

Express 3.0和4.0使用不同的语法启用http压缩,请参阅下面的gasolin发布。

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

这篇关于快速gzip静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆