Node.js compression middleware.Node.js压缩中间件。
The following compression codings are supported:支持以下压缩编码:
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
var compression = require('compression')
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
头的响应,因为压缩将转换主体。
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
对象。
The default value is 默认值为zlib.Z_DEFAULT_CHUNK
, or 16384
.zlib.Z_DEFAULT_CHUNK
,或16384
。
See Node.js documentation regarding the usage.有关用法,请参阅Node.js文档。
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')是否可压缩。res.getHeader('Content-Type')
is compressible.
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)。
-1
zlib.Z_DEFAULT_COMPRESSION
).zlib.Z_DEFAULT_COMPRESSION
)。0
zlib.Z_NO_COMPRESSION
).zlib.Z_NO_COMPRESSION
)。1
zlib.Z_BEST_SPEED
).zlib.Z_BEST_SPEED
)。2
3
4
5
6
zlib.Z_DEFAULT_COMPRESSION
points to).zlib.Z_DEFAULT_COMPRESSION
指向的内容)。7
8
9
zlib.Z_BEST_COMPRESSION
).zlib.Z_BEST_COMPRESSION
)。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')
。
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文档。
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.此值仅影响压缩比,而不影响压缩输出的正确性,即使未正确设置。
zlib.Z_DEFAULT_STRATEGY
zlib.Z_FILTERED
zlib.Z_DEFAULT_STRATEGY
and zlib.Z_HUFFMAN_ONLY
.zlib.Z_DEFAULT_STRATEGY
和zlib.Z_HUFFMAN_ONLY
之间处于中间位置。zlib.Z_FIXED
zlib.Z_HUFFMAN_ONLY
zlib.Z_RLE
zlib.Z_HUFFMAN_ONLY
, but give better compression for PNG image data.zlib.Z_HUFFMAN_ONLY
一样快,但可以更好地压缩PNG图像数据。Note in the list above, 注意,在上面的列表中,zlib
is from zlib = require('zlib')
.zlib
来自zlib = require('zlib')
。
The byte threshold for the response body size before compression is considered for the response, defaults to 响应考虑压缩前响应正文大小的字节阈值,默认为1kb
. 1kb
。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 为了保证可以确定响应大小,请确保设置Content-Length
response header.Content-Length
响应头。
The default value is 默认值为zlib.Z_DEFAULT_WINDOWBITS
, or 15
.zlib.Z_DEFAULT_WINDOWBITS
,或15
。
See Node.js documentation regarding the usage.有关用法,请参阅Node.js文档。
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)
}
This module adds a 此模块添加res.flush()
method to force the partially-compressed response to be flushed to the client.res.flush()
方法,以强制将部分压缩的响应刷新到客户端。
When using this module with express or connect, simply 当将此模块与express或connect一起使用时,只需要通过app.use
the module as high as you like. 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
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)
})
})