What is compression

compression

Node.js compression middleware.

The following compression codings are supported:

Install

This is a Node.js module available through the npm registry. Installation is done using the command:

$ npm install compression

API

var compression =require('compression')

compression([options])

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

This middleware will never compress responses that include a header with the directive, as compressing will transform the body.

Options

accepts these properties in the options object. In addition to those listed below, zlib options may be passed in to the options object.

chunkSize

The default value is , or .

See Node.js documentation regarding the usage.

filter

A function to decide if the response should be considered for compression. This function is called as and is expected to return to consider the response for compression, or to not compress the response.

The default filter function uses the compressible module to determine if is compressible.

level

The level of zlib compression to apply to responses. A higher level will result in better compression, but will take longer to complete. A lower level will result in less compression, but will be much faster.

This is an integer in the range of (no compression) to (maximum compression). The special value can be used to mean the "default compression level", which is a default compromise between speed and compression (currently equivalent to level 6).

  • Default compression level (also ).
  • No compression (also ).
  • Fastest compression (also ).
  • (currently what points to).
  • Best compression (also ).

The default value is , or .

Note in the list above, is from .

memLevel

This specifies how much memory should be allocated for the internal compression state and is an integer in the range of (minimum level) and (maximum level).

The default value is , or .

See Node.js documentation regarding the usage.

strategy

This is used to tune the compression algorithm. This value only affects the compression ratio, not the correctness of the compressed output, even if it is not set appropriately.

  • Use for normal data.
  • Use for data produced by a filter (or predictor). Filtered data consists mostly of small values with a somewhat random distribution. In this case, the compression algorithm is tuned to compress them better. The effect is to force more Huffman coding and less string matching; it is somewhat intermediate between and .
  • Use to prevent the use of dynamic Huffman codes, allowing for a simpler decoder for special applications.
  • Use to force Huffman encoding only (no string match).
  • Use to limit match distances to one (run-length encoding). This is designed to be almost as fast as , but give better compression for PNG image data.

Note in the list above, is from .

threshold

The byte threshold for the response body size before compression is considered for the response, defaults to . This is a number of bytes or any string accepted by the bytes module.

Note this is only an advisory setting; if the response size cannot be determined at the time the response headers are written, then it is assumed the response is over the threshold. To guarantee the response size can be determined, be sure set a response header.

windowBits

The default value is , or .

See Node.js documentation regarding the usage.

.filter

The default function. This is used to construct a custom filter function that is an extension of the default function.

var compression =require('compression')

var express =require('express')

var app =express()

app.use(compression({ filter: shouldCompress }))

functionshouldCompress(req,res){

if(req.headers['x-no-compression']){

returnfalse

}

returncompression.filter(req, res)

}

res.flush

This module adds a method to force the partially-compressed response to be flushed to the client.

Examples

express/connect

When using this module with express or connect, simply the module as high as you like. Requests that pass through the middleware will be compressed.

var compression =require('compression')

var express =require('express')

var app =express()

app.use(compression())

Server-Sent Events

Because of the nature of compression this module does not work out of the box with server-sent events. To compress content, a window of the output needs to be buffered up in order to get good compression. Typically when using server-sent events, there are certain block of data that need to reach the client.

You can achieve this by calling when you need the data written to actually make it to the client.

var compression =require('compression')

var express =require('express')

var app =express()

app.use(compression())

app.get('/events',function(req,res){

res.setHeader('Content-Type','text/event-stream')

res.setHeader('Cache-Control','no-cache')

var timer =setInterval(function(){

res.write('data: ping\n\n')

res.flush()

},2000)

res.on('close',function(){

clearInterval(timer)

})

})

License

MIT