Node.js: Gzip compression?

前端 未结 13 1362
迷失自我
迷失自我 2020-11-30 21:57

Am I wrong in finding that Node.js does no gzip compression and there are no modules out there to perform gzip compression? How can anyone use a web server that has no compr

相关标签:
13条回答
  • 2020-11-30 22:34

    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/

    0 讨论(0)
  • 2020-11-30 22:35

    Generally speaking, for a production web application, you will want to put your node.js app behind a lightweight reverse proxy such as nginx or lighttpd. Among the many benefits of this setup, you can configure the reverse proxy to do http compression or even tls compression, without having to change your application source code.

    0 讨论(0)
  • 2020-11-30 22:38

    While as others have right pointed out using a front end webserver such as nginx can handle this implicitly, another option, is to use nodejitsu's excellent node-http-proxy to serve up your assets.

    eg:

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

    This example demonstrates support for gzip compression through the use of connect middleware module: connect-gzip.

    0 讨论(0)
  • 2020-11-30 22:39

    If you're using Express, then you can use its compress method as part of the configuration:

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

    And you can find more on compress here: http://expressjs.com/api.html#compress

    And if you're not using Express... Why not, man?! :)

    NOTE: (thanks to @ankitjaininfo) This middleware should be one of the first you "use" to ensure all responses are compressed. Ensure that this is above your routes and static handler (eg. how I have it above).

    NOTE: (thanks to @ciro-costa) Since express 4.0, the express.compress middleware is deprecated. It was inherited from connect 3.0 and express no longer includes connect 3.0. Check Express Compression for getting the middleware.

    0 讨论(0)
  • 2020-11-30 22:41

    1- Install compression

    npm install compression
    

    2- Use it

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

    compression on Github

    0 讨论(0)
  • 2020-11-30 22:42

    Node v0.6.x has a stable zlib module in core now - there are some examples on how to use it server-side in the docs too.

    An example (taken from the docs):

    // 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);
    
    0 讨论(0)
提交回复
热议问题