Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js: Gzip compression?

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 compression? What am I missing here? Should I try to—gasp—port the algorithm to JavaScript for server-side use?

like image 235
SnickersAreMyFave Avatar asked Oct 08 '10 22:10

SnickersAreMyFave


People also ask

What is gzip compression in node JS?

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.

How do I compress data in node JS?

There are two methods of compression. One is calling it directly in your Node. js app using the compression middleware, and the other is to use it at a reverse proxy level through software like NGINX.

What is gzip compression?

What Is GZIP Compression? GZIP is a compression technology frequently used for transferring data quickly over the internet. “GZIP” refers to a compression method, software used to compress files with this method, and the file format that results from GZIP compression (usually indicated by the file extension . gz).

What is compression NPM?

compression([options]) Returns the compression middleware using the given options . The middleware will attempt to compress response bodies for all request that traverse through the middleware, based on the given options .


2 Answers

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); 
like image 192
hughsk Avatar answered Oct 01 '22 11:10

hughsk


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.

like image 35
Milimetric Avatar answered Oct 01 '22 12:10

Milimetric