English 中文(简体)
Node.js:Gzip压缩?
原标题:Node.js: Gzip compression?

我发现Node.js没有gzip压缩,也没有模块来执行gzip压缩是不是错了?任何人怎么能使用没有压缩的网络服务器?我在这里错过了什么?我应该尝试将算法移植到JavaScript以供服务器端使用吗?

问题回答

节点v0.6.x具有稳定的zlib模块现在在core中-文档中也有一些关于如何在服务器端使用它的示例。

一个例子(取自文档):

// server example
// Running a gzip operation on every request is quite expensive.
// It would be much more efficient to cache the compressed buffer.
var zlib = require( zlib );
var http = require( http );
var fs = require( fs );
http.createServer(function(request, response) {
  var raw = fs.createReadStream( index.html );
  var acceptEncoding = request.headers[ accept-encoding ];
  if (!acceptEncoding) {
    acceptEncoding =   ;
  }

  // Note: this is not a conformant accept-encoding parser.
  // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
  if (acceptEncoding.match(/deflate/)) {
    response.writeHead(200, {  content-encoding :  deflate  });
    raw.pipe(zlib.createDeflate()).pipe(response);
  } else if (acceptEncoding.match(/gzip/)) {
    response.writeHead(200, {  content-encoding :  gzip  });
    raw.pipe(zlib.createGzip()).pipe(response);
  } else {
    response.writeHead(200, {});
    raw.pipe(response);
  }
}).listen(1337);

如果您正在使用Express,然后您可以使用其压缩方法作为配置的一部分:

var express = require( express );
var app = express.createServer();
app.use(express.compress());

您可以在这里找到更多关于压缩的信息:http://expressjs.com/api.html#compress

如果您不使用快递…为什么不呢,伙计?!:)

注意:(感谢@ankitjaininfo)这个中间件应该是第一个“使用”来确保所有响应都被压缩的中间件之一。确保这是在你的路由和静态处理程序之上(例如,我如何拥有它上面)。

注意:(感谢@ciro-costa)自express4.0以来,express.compress中间件已被弃用。它继承自connect 3.0,express不再包含connect 3.0。检查用于获取中间件的快速压缩

1-安装压缩

npm install compression

2-使用它

var express     = require( express )
var compression = require( compression )

var app = express()
app.use(compression())

Github上的压缩

一般来说,对于生产web应用程序,您会希望将node.js应用程序放在一个轻量级的反向代理(如nginx或lighttpd)后面。在这种设置的众多好处中,您可以配置反向代理进行http压缩甚至tls压缩,而无需更改应用程序源代码。

尽管您可以使用反向代理进行gzip,如nginx、lighttpd或in varnish。在应用程序级别进行大多数http优化(如gzip)是有益的,这样您就可以对gzip的资产有一个更精细的方法。

实际上,我已经为expressjs/connect创建了自己的gzip模块,名为gzippohttps://github.com/tomgco/gzippo尽管是新的,但它确实能胜任这项工作。此外,它使用节点压缩,而不是生成unix gzip命令。

为了压缩文件,您可以使用以下代码

var fs = require("fs");
var zlib = require( zlib );
fs.createReadStream( input.txt ).pipe(zlib.createGzip())
.pipe(fs.createWriteStream( input.txt.gz ));
console.log("File Compressed.");

为了解压缩相同的文件,您可以使用以下代码

var fs = require("fs");
var zlib = require( zlib );
fs.createReadStream( input.txt.gz )
.pipe(zlib.createGunzip())
.pipe(fs.createWriteStream( input.txt ));
console.log("File Decompressed.");

即使您没有使用express,您仍然可以使用他们的中间件。压缩模块是我正在使用的:

var http = require( http )
var fs = require( fs )
var compress = require("compression")
http.createServer(function(request, response) {
  var noop = function(){}, useDefaultOptions = {}
  compress(useDefaultOptions)(request,response,noop) // mutates the response object

  response.writeHead(200)
  fs.createReadStream( index.html ).pipe(response)
}).listen(1337)

Use gzip compression

Gzip压缩可以大大减小响应体的大小,从而提高网络应用程序的速度。在Express应用程序中使用压缩中间件进行gzip压缩。例如:

var compression = require( compression );
var express = require( express )
var app = express()
app.use(compression())

正如其他人正确指出的那样,使用nginx等前端Web服务器可以隐式处理此问题,但另一种选择是使用柔术s优秀节点http proxy来提供您的资产。

例如:

httpProxy.createServer(
 require( connect-gzip ).gzip(),
 9000,  localhost 
).listen(8000);

此示例通过使用连接中间件模块:连接gzip

不如这个

node-compress
A streaming compression / gzip module for node.js
To install, ensure that you have libz installed, and run:
node-waf configure
node-waf build
This will put the compress.node binary module in build/default.
...

There are multiple Gzip middlewares for Express, KOA and others. For example: https://www.npmjs.com/package/express-static-gzip

However, Node is awfully bad at doing CPU intensive tasks like gzipping, SSL termination, etc. Instead, use a ‘real’ middleware services like nginx or HAproxy, see bullet 3 here: http://goldbergyoni.com/checklist-best-practice-of-node-js-in-production/

截至目前,epxress.compress()似乎在这方面做得很出色。

在任何express应用程序中,只需调用this.use(express.compress())

我亲自在快车上驾驶机车,这辆车运行得很好。我无法与任何其他建立在express之上的库或框架交谈,但只要它们尊重全栈透明性,你就应该没事。

使用node已经有好几天了,您可以正确地说,没有gzip就无法创建Web服务器。

Node.js Wiki上的模块页面上提供了很多选项。我试过了大部分,但这是我最终使用的-

https://github.com/donnerjack13589/node.gzip

v1.0也已经发布,到目前为止它已经相当稳定了。





相关问题
BlackBerry - Unpack Zip File

I m developing a BlackBerry application in which I need to unpack a zip file compressed with PKZIP. The package could have one file in it, or it could have 10; it will vary in each case. I know that ...

How to find if a request is for js or css in httpHandler

is there any way to find if a particular request is for JS or CSS in httphandler to improve the performance of my website i was using HttpCompress from Code Project http://www.codeproject.com/KB/...

Compress data before storage on Google App Engine

I im trying to store 30 second user mp3 recordings as Blobs in my app engine data store. However, in order to enable this feature (App Engine has a 1MB limit per upload) and to keep the costs down I ...

How to process compressed data in Java

I have some data which takes up more than 50MB in an uncompressed file, but compresses down to less than half a MB using gzip. Most of this is numerical data. I m trying to figure out how to process ...

What is the best way to extract a zip file using java

I have a a zipped file. That file contains various directories and files also. I want to extract all those and save in a specified path. So How to write a java program to extract the zipped file. ...