Node.js: Gzip compression?

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

提交回复
热议问题