3 [![NPM Version][npm-image]][npm-url]
4 [![NPM Downloads][downloads-image]][downloads-url]
5 [![Build Status][travis-image]][travis-url]
6 [![Test Coverage][coveralls-image]][coveralls-url]
7 [![Gratipay][gratipay-image]][gratipay-url]
9 Node.js compression middleware.
11 The following compression codings are supported:
19 $ npm install compression
25 var compression = require('compression')
28 ### compression([options])
30 Returns the compression middleware using the given `options`.
34 `compression()` accepts these properties in the options object. In addition to
35 those listed below, [zlib](http://nodejs.org/api/zlib.html) options may be
36 passed in to the options object.
40 The default value is `zlib.Z_DEFAULT_CHUNK`, or `16384`.
42 See [Node.js documentation](http://nodejs.org/api/zlib.html#zlib_memory_usage_tuning)
47 A function to decide if the response should be considered for compression.
48 This function is called as `filter(req, res)` and is expected to return
49 `true` to consider the response for compression, or `false` to not compress
52 The default filter function uses the [compressible](https://www.npmjs.com/package/compressible)
53 module to determine if `res.getHeader('Content-Type')` is compressible.
57 The level of zlib compression to apply to responses. A higher level will result
58 in better compression, but will take longer to complete. A lower level will
59 result in less compression, but will be much faster.
61 This is an integer in the range of `0` (no compression) to `9` (maximum
62 compression). The special value `-1` can be used to mean the "default
63 compression level", which is a default compromise between speed and
64 compression (currently equivalent to level 6).
66 - `-1` Default compression level (also `zlib.Z_DEFAULT_COMPRESSION`).
67 - `0` No compression (also `zlib.Z_NO_COMPRESSION`).
68 - `1` Fastest compression (also `zlib.Z_BEST_SPEED`).
73 - `6` (currently what `zlib.Z_DEFAULT_COMPRESSION` points to).
76 - `9` Best compression (also `zlib.Z_BEST_COMPRESSION`).
78 The default value is `zlib.Z_DEFAULT_COMPRESSION`, or `-1`.
80 **Note** in the list above, `zlib` is from `zlib = require('zlib')`.
84 This specifies how much memory should be allocated for the internal compression
85 state and is an integer in the range of `1` (minimum level) and `9` (maximum
88 The default value is `zlib.Z_DEFAULT_MEMLEVEL`, or `8`.
90 See [Node.js documentation](http://nodejs.org/api/zlib.html#zlib_memory_usage_tuning)
95 This is used to tune the compression algorithm. This value only affects the
96 compression ratio, not the correctness of the compressed output, even if it
97 is not set appropriately.
99 - `zlib.Z_DEFAULT_STRATEGY` Use for normal data.
100 - `zlib.Z_FILTERED` Use for data produced by a filter (or predictor).
101 Filtered data consists mostly of small values with a somewhat random
102 distribution. In this case, the compression algorithm is tuned to
103 compress them better. The effect is to force more Huffman coding and less
104 string matching; it is somewhat intermediate between `zlib.Z_DEFAULT_STRATEGY`
105 and `zlib.Z_HUFFMAN_ONLY`.
106 - `zlib.Z_FIXED` Use to prevent the use of dynamic Huffman codes, allowing
107 for a simpler decoder for special applications.
108 - `zlib.Z_HUFFMAN_ONLY` Use to force Huffman encoding only (no string match).
109 - `zlib.Z_RLE` Use to limit match distances to one (run-length encoding).
110 This is designed to be almost as fast as `zlib.Z_HUFFMAN_ONLY`, but give
111 better compression for PNG image data.
113 **Note** in the list above, `zlib` is from `zlib = require('zlib')`.
117 The byte threshold for the response body size before compression is considered
118 for the response, defaults to `1kb`. This is a number of bytes, any string
119 accepted by the [bytes](https://www.npmjs.com/package/bytes) module, or `false`.
121 **Note** this is only an advisory setting; if the response size cannot be determined
122 at the time the response headers are written, then it is assumed the response is
123 _over_ the threshold. To guarantee the response size can be determined, be sure
124 set a `Content-Length` response header.
128 The default value is `zlib.Z_DEFAULT_WINDOWBITS`, or `15`.
130 See [Node.js documentation](http://nodejs.org/api/zlib.html#zlib_memory_usage_tuning)
135 The default `filter` function. This is used to construct a custom filter
136 function that is an extension of the default function.
139 app.use(compression({filter: shouldCompress}))
141 function shouldCompress(req, res) {
142 if (req.headers['x-no-compression']) {
143 // don't compress responses with this request header
147 // fallback to standard filter function
148 return compression.filter(req, res)
154 This module adds a `res.flush()` method to force the partially-compressed
155 response to be flushed to the client.
161 When using this module with express or connect, simply `app.use` the module as
162 high as you like. Requests that pass through the middleware will be compressed.
165 var compression = require('compression')
166 var express = require('express')
170 // compress all requests
171 app.use(compression())
176 ### Server-Sent Events
178 Because of the nature of compression this module does not work out of the box
179 with server-sent events. To compress content, a window of the output needs to
180 be buffered up in order to get good compression. Typically when using server-sent
181 events, there are certain block of data that need to reach the client.
183 You can achieve this by calling `res.flush()` when you need the data written to
184 actually make it to the client.
187 var compression = require('compression')
188 var express = require('express')
192 // compress responses
193 app.use(compression())
195 // server-sent event stream
196 app.get('/events', function (req, res) {
197 res.setHeader('Content-Type', 'text/event-stream')
198 res.setHeader('Cache-Control', 'no-cache')
200 // send a ping approx every 2 seconds
201 var timer = setInterval(function () {
202 res.write('data: ping\n\n')
204 // !!! this is the important part
208 res.on('close', function () {
218 [npm-image]: https://img.shields.io/npm/v/compression.svg
219 [npm-url]: https://npmjs.org/package/compression
220 [travis-image]: https://img.shields.io/travis/expressjs/compression/master.svg
221 [travis-url]: https://travis-ci.org/expressjs/compression
222 [coveralls-image]: https://img.shields.io/coveralls/expressjs/compression/master.svg
223 [coveralls-url]: https://coveralls.io/r/expressjs/compression?branch=master
224 [downloads-image]: https://img.shields.io/npm/dm/compression.svg
225 [downloads-url]: https://npmjs.org/package/compression
226 [gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg
227 [gratipay-url]: https://www.gratipay.com/dougwilson/