首页 文章

Node.js:Gzip压缩?

提问于
浏览
76

我错误地发现Node.js没有gzip压缩,并且没有模块可以执行gzip压缩吗?如何使用没有压缩的Web服务器?我在这里错过了什么?我是否应该尝试将算法加载到JavaScript以供服务器端使用?

14 回答

  • 1

    虽然你可以使用反向代理gzip,如nginx,lighttpd或清漆 . 在应用程序级别进行大多数http优化(例如gzipping)可能会很有用,这样您就可以对gzip的资产进行更细粒度的处理 .

    我实际上为expressjs / connect创建了我自己的gzip模块,名为gzippo https://github.com/tomgco/gzippo虽然它确实有新功能 . 另外,它使用node-compress而不是产生unix gzip命令 .

  • 2

    虽然正如其他人所指出的那样使用前端网络服务器(例如 nginx )可以隐式处理这个问题,但另一个选择是使用nodejitsu's excellent node-http-proxy来提供资产 .

    例如:

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

    此示例通过使用connect middleware module: connect-gzip 演示了对gzip压缩的支持 .

  • 1

    this怎么样?

    node-compress node.js的流压缩/ gzip模块要安装,请确保安装了libz,并运行:node-waf configure node-waf build这将使compress.node二进制模块处于build / default . ...

  • 2

    这里列出了modules for compression的数量:

  • 28

    Node v0.6.x现在在核心中有一个稳定的zlib module - 在文档中也有一些关于如何在服务器端使用它的例子 .

    一个例子(取自文档):

    // 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(/\bdeflate\b/)) {
        response.writeHead(200, { 'content-encoding': 'deflate' });
        raw.pipe(zlib.createDeflate()).pipe(response);
      } else if (acceptEncoding.match(/\bgzip\b/)) {
        response.writeHead(200, { 'content-encoding': 'gzip' });
        raw.pipe(zlib.createGzip()).pipe(response);
      } else {
        response.writeHead(200, {});
        raw.pipe(response);
      }
    }).listen(1337);
    
  • 3

    如果您正在使用Express,则可以将其compress方法用作配置的一部分:

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

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

    如果你不使用Express ......为什么不呢,伙计?! :)

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

    注意:(感谢@ ciro-costa)从Express 4.0开始,不推荐使用 express.compress 中间件 . 它继承自connect 3.0并且express不再包含connect 3.0 . 检查Express Compression以获取中间件 .

  • 41

    1-安装 compression

    npm install compression
    

    2-使用它

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

    compression on Github

  • 56

    一般来说,对于 生产环境 Web应用程序,您需要将node.js应用程序放在轻量级反向代理(例如nginx或lighttpd)之后 . 在此设置的众多优点中,您可以将反向代理配置为进行http压缩甚至压缩,而无需更改应用程序源代码 .

  • 2

    即使您没有使用express,您仍然可以使用他们的中间件 . 我正在使用compression module

    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)
    
  • 8

    要压缩文件,您可以使用下面的代码

    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.");
    
  • 1

    节点已经过了好几天了,你可以说没有gzip就无法创建网络服务器 .

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

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

    v1.0也出局了,到目前为止一直很稳定 .

  • 1

    截至今天,epxress.compress()似乎做得非常出色 .

    在任何快递应用程序中只需调用this.use(express.compress());我亲自在快递上运行机车,这很漂亮 . 我不能谈论 Build 在快递之上的任何其他库或框架,但只要他们尊重完整的堆栈透明度,你应该没问题 .

  • 40

    Express,KOA等有多个Gzip中间件 . 例如:https://www.npmjs.com/package/express-static-gzip

    但是,Node在执行CPU密集型任务(例如gzipping,SSL终止等)方面非常糟糕 . 相反,使用“真正的”中间件服务(如nginx或HAproxy),请参阅此处的项目符号3:http://goldbergyoni.com/checklist-best-practice-of-node-js-in-production/

  • 64

    使用gzip压缩

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

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

相关问题