compression

NPM Version NPM Downloads Build Status Test Coverage

Node.js compression middleware.Node.js压缩中间件。

The following compression codings are supported:支持以下压缩编码:

Install安装

This is a Node.js module available through the npm registry. 这是一Node.js模块,可通过npm注册表获得。Installation is done using the npm install command:使用npm install命令完成安装:

$ npm install compression

API

var compression = require('compression')

compression([options])

Returns the compression middleware using the given options. 使用给定的options返回压缩中间件。The middleware will attempt to compress response bodies for all request that traverse through the middleware, based on the given options.中间件将根据给定的options,尝试压缩通过中间件的所有请求的响应体。

This middleware will never compress responses that include a Cache-Control header with the no-transform directive, as compressing will transform the body.该中间件永远不会压缩包含no-transform指令Cache-Control头的响应,因为压缩将转换主体。

Options

compression() accepts these properties in the options object. compression()接受选项对象中的这些属性。In addition to those listed below, zlib options may be passed in to the options object.除了下面列出的选项外,zlib选项还可以传递给options对象。

chunkSize

The default value is zlib.Z_DEFAULT_CHUNK, or 16384.默认值为zlib.Z_DEFAULT_CHUNK,或16384

See Node.js documentation regarding the usage.有关用法,请参阅Node.js文档

filter

A function to decide if the response should be considered for compression. 决定是否应考虑压缩响应的函数。This function is called as filter(req, res) and is expected to return true to consider the response for compression, or false to not compress the response.这个函数以filter(req, res)的形式调用,并且预期返回true以考虑压缩响应,或者错误地不压缩响应。

The default filter function uses the compressible module to determine if res.getHeader('Content-Type') is compressible.默认过滤器函数使用可压缩模块确定res.getHeader('Content-Type')是否可压缩。

level

The level of zlib compression to apply to responses. 应用于响应的zlib压缩级别。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 0 (no compression) to 9 (maximum compression). 这是一个介于0(无压缩)到9(最大压缩)之间的整数。The special value -1 can be used to mean the "default compression level", which is a default compromise between speed and compression (currently equivalent to level 6).特殊值-1可用于表示“默认压缩级别”,这是速度和压缩之间的默认折衷(目前相当于级别6)。

The default value is zlib.Z_DEFAULT_COMPRESSION, or -1.默认值为zlib.Z_DEFAULT_COMPRESSION,或-1

Note in the list above, zlib is from zlib = require('zlib').注意,在上面的列表中,zlib来自zlib = require('zlib')

memLevel

This specifies how much memory should be allocated for the internal compression state and is an integer in the range of 1 (minimum level) and 9 (maximum level).这指定应为内部压缩状态分配多少内存,并且是1(最低级别)和9(最高级别)范围内的整数。

The default value is zlib.Z_DEFAULT_MEMLEVEL, or 8.默认值为zlib.Z_default_MEMLEVEL,或8

See Node.js documentation regarding the usage.有关用法,请参阅Node.js文档

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.此值仅影响压缩比,而不影响压缩输出的正确性,即使未正确设置。

Note in the list above, zlib is from zlib = require('zlib').注意,在上面的列表中,zlib来自zlib = require('zlib')

threshold

The byte threshold for the response body size before compression is considered for the response, defaults to 1kb. 响应考虑压缩前响应正文大小的字节阈值,默认为1kbThis 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 Content-Length response header.为了保证可以确定响应大小,请确保设置Content-Length响应头。

windowBits

The default value is zlib.Z_DEFAULT_WINDOWBITS, or 15.默认值为zlib.Z_DEFAULT_WINDOWBITS,或15

See Node.js documentation regarding the usage.有关用法,请参阅Node.js文档

.filter

The default filter function. 默认的filter函数。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 }))

function shouldCompress (req, res) {
  if (req.headers['x-no-compression']) {
    // don't compress responses with this request header
    return false
  }

  // fallback to standard filter function
  return compression.filter(req, res)
}

res.flush

This module adds a res.flush() method to force the partially-compressed response to be flushed to the client.此模块添加res.flush()方法,以强制将部分压缩的响应刷新到客户端。

Examples

express/connect

When using this module with express or connect, simply app.use the module as high as you like. 当将此模块与express或connect一起使用时,只需要通过app.use调用你想要的模块。Requests that pass through the middleware will be compressed.通过中间件的请求将被压缩。

var compression = require('compression')
var express = require('express')

var app = express()

// compress all responses
app.use(compression())

// add all routes

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 res.flush() when you need the data written to actually make it to the client.当需要将数据写入客户机时,可以通过调用res.flush()来实现这一点。

var compression = require('compression')
var express = require('express')

var app = express()

// compress responses
app.use(compression())

// server-sent event stream
app.get('/events', function (req, res) {
  res.setHeader('Content-Type', 'text/event-stream')
  res.setHeader('Cache-Control', 'no-cache')

  // send a ping approx every 2 seconds
  var timer = setInterval(function () {
    res.write('data: ping\n\n')

    // !!! this is the important part
    res.flush()
  }, 2000)

  res.on('close', function () {
    clearInterval(timer)
  })
})

License

MIT