Javascript 表达 gzip 静态内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6370478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Express gzip static content
提问by HyderA
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?
Express 和 connect 似乎已经删除了它们的 gzip 功能,因为它们太低效了。目前是否有使用 express-js 进行 gzip 的可靠解决方案?
采纳答案by Steven de Salas
Connect 2.0has added support for compress()middleware based on the new zlib stuffwith that has just come out in Node Core API.
Connect 2.0添加了对compress()中间件的支持,该中间件基于Node Core API 中刚刚出现的新 zlib 内容。
You can make use of this in your express server by adding a dependency to connect 2.0 in your package.json
file:
您可以通过在您的package.json
文件中添加一个连接 2.0 的依赖项,在您的 Express 服务器中使用它:
{
...
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 newand while I could get it to work locally, my Herokucloud application complained about the dependency on Compress 2.x during the pre-commit hook when deploying via git:
请注意,这些东西仍然很新,虽然我可以让它在本地工作,但我的Heroku云应用程序在通过 git 部署时在 pre-commit 钩子期间抱怨对 Compress 2.x 的依赖:
-----> 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).
如您所见,他们仍在使用旧版本的节点 (0.4.7)。
UPDATE:
更新:
Actually, I could get Heroku to deploy this by adding the corresponding engines
section in the package.json
:
实际上,我可以通过在以下engines
内容中添加相应的部分来让 Heroku 部署它package.json
:
{
...
"engines": {
"node": ">= 0.6.0 < 0.7.0"
}
}
And these are the results when using a http compression tester:
这些是使用 http 压缩测试器时的结果:
UPDATE June 2014
2014 年 6 月更新
Hiya, if you are reading this now. Dont forget that the stuff above is only relevant to Express 2.0.
嗨,如果你现在正在阅读这篇文章。不要忘记上面的内容只与 Express 2.0 相关。
Express 3.0 and 4.0 use different syntax for enabling http compression, see post by gasolin just below.
Express 3.0 和 4.0 使用不同的语法来启用 http 压缩,请参阅下面 gasolin 的帖子。
回答by gasolin
Express 3.0 now has compress() support:
Express 3.0 现在有 compress() 支持:
var app = express();
// gzip
app.use(express.compress());
// static
app.use("/public", express.static(__dirname + '/public'));
// listen
app.listen(80);
EDITfor Express 4.0, compress become the separate middleware. So you have to install and import to use it:
编辑Express 4.0,压缩成为单独的中间件。所以你必须安装并导入才能使用它:
var compress = require('compression');
app.use(compress());
回答by Alfred
I have also searched npm and found for example:
我还搜索了 npm 并找到了例如:
https://github.com/tomgallacher/gzippo
gzippo pronounced g-zippo is a gzip middleware for Connect using Compress for better performance.
https://github.com/tomgallacer/gzippo
gzippo 发音为 g-zippo 是一个 gzip 中间件,用于使用压缩进行连接以获得更好的性能。
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.
最近开发了 Gzippo(2 天前),我认为这是一件好事。我不能告诉你生产使用情况。您应该自己测试/基准测试。我也可能会使用 CDN 用于实时站点或 Nginx 来托管我的静态文件,而不是一些 nodejs 模块。
回答by tjholowaychuk
Connect will support the new zlib stuff in Node in the next release
Connect 将在下一个版本中支持 Node 中的新 zlib 内容
回答by Raynos
If you've searched the npm you may have come across node-compress.
如果您搜索过 npm,您可能会遇到node-compress。
It shouldn't be too hard to inject it as middleware into express.
将它作为中间件注入 express 应该不会太难。