Node.js: Gzip compression?

前端 未结 13 1364
迷失自我
迷失自我 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:46

    How about this?

    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.
    ...

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

    As of today, epxress.compress() seems to be doing a brilliant job of this.

    In any express app just call this.use(express.compress());.

    I'm running locomotive on top of express personally and this is working beautifully. I can't speak to any other libraries or frameworks built on top of express but as long as they honor full stack transparency you should be fine.

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

    Use gzip compression

    Gzip compressing can greatly decrease the size of the response body and hence increase the speed of a web app. Use the compression middleware for gzip compression in your Express app. For example:

    var compression = require('compression');
    var express = require('express')
    var app = express()
    app.use(compression())
    
    0 讨论(0)
  • 2020-11-30 22:49

    It's been a few good days with node, and you're right to say that you can't create a webserver without gzip.

    There are quite a lot options given on the modules page on the Node.js Wiki. I tried out most of them, but this is the one which I'm finally using -

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

    v1.0 is also out and it has been quite stable so far.

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

    Even if you're not using express, you can still use their middleware. The compression module is what I'm using:

    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)
    
    0 讨论(0)
  • 2020-11-30 22:57

    Although you can gzip using a reverse proxy such as nginx, lighttpd or in varnish. It can be beneficial to have most http optimisations such as gzipping at the application level so that you can have a much granular approach on what asset's to gzip.

    I have actually created my own gzip module for expressjs / connect called gzippo https://github.com/tomgco/gzippo although new it does do the job. Plus it uses node-compress instead of spawning the unix gzip command.

    0 讨论(0)
提交回复
热议问题