简体   繁体   English

快递gzip静态内容

[英]Express gzip static content

Express and connect appeared to have removed their gzip functions because they were too inefficient. Express和connect似乎已经删除了他们的gzip函数,因为它们效率太低。 Are there any reliable solutions to gzip with express-js currently? 目前有没有可靠的gzip与express-js的解决方案?

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

EDIT for Express 4.0, compress become the separate middleware. 编辑 Express 4.0,压缩成为独立的中间件。 So you have to install and import to use it: 所以你必须安装并导入才能使用它:

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

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. Connect 2.0增加了对基于新的zlib内容的 compress()中间件的支持, 这些内容刚刚在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: 您可以通过在package.json文件中添加依赖关系来在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 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云应用程序抱怨在通过git部署时在预提交钩子期间对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 : 实际上,我可以通过在package.json添加相应的engines部分来让Heroku部署它:

{
    ...
    "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帖子。

I have also searched npm and found for example: 我也搜索了npm并找到了例如:

Gzippo has recently been developed(2 days ago) which I think is a good thing. 最近开发了Gzippo(2天前),我认为这是一件好事。 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. 我也可能使用CDN作为实时站点或Nginx来托管我的静态文件而不是某些nodejs模块。

Connect将在下一版本中支持Node中的新zlib内容

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中应该不会太难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM