What is koa-compress?
The koa-compress package is a middleware for the Koa framework that provides HTTP compression. It supports various compression algorithms like gzip, deflate, and brotli, which can significantly reduce the size of the response body and improve the performance of web applications.
What are koa-compress's main functionalities?
Basic Gzip Compression
This code demonstrates how to set up basic gzip compression in a Koa application using the koa-compress middleware. The middleware is added to the Koa app, and it will automatically compress the response body using gzip.
const Koa = require('koa');
const compress = require('koa-compress');
const app = new Koa();
app.use(compress());
app.use(ctx => {
ctx.body = 'Hello World';
});
app.listen(3000);
Custom Compression Options
This code shows how to configure custom options for the compression middleware. The 'threshold' option specifies the minimum response size in bytes to compress, and the 'flush' option is used to control the zlib flush mode.
const Koa = require('koa');
const compress = require('koa-compress');
const app = new Koa();
app.use(compress({
threshold: 2048,
flush: require('zlib').Z_SYNC_FLUSH
}));
app.use(ctx => {
ctx.body = 'Hello World';
});
app.listen(3000);
Brotli Compression
This code demonstrates how to enable Brotli compression with custom parameters. The 'br' option is used to configure Brotli-specific settings, such as the compression quality.
const Koa = require('koa');
const compress = require('koa-compress');
const zlib = require('zlib');
const app = new Koa();
app.use(compress({
br: {
params: {
[zlib.constants.BROTLI_PARAM_QUALITY]: 4
}
}
}));
app.use(ctx => {
ctx.body = 'Hello World';
});
app.listen(3000);
Other packages similar to koa-compress
compression
The 'compression' package is a middleware for Express.js that provides HTTP compression. It supports gzip and deflate algorithms. Compared to koa-compress, it is designed specifically for Express.js and does not support Brotli compression out of the box.
shrink-ray-current
The 'shrink-ray-current' package is a middleware for Node.js that provides HTTP compression using gzip, deflate, and Brotli algorithms. It is similar to koa-compress in terms of supported algorithms but can be used with various frameworks, not just Koa.
Koa Compress
Compress middleware for Koa
Example
var compress = require('koa-compress')
var Koa = require('koa')
var app = new Koa()
app.use(compress({
filter: function (content_type) {
return /text/i.test(content_type)
},
threshold: 2048,
flush: require('zlib').Z_SYNC_FLUSH
}))
Options
The options are passed to zlib
: http://nodejs.org/api/zlib.html#zlib_options
filter
An optional function that checks the response content type to decide whether to compress.
By default, it uses compressible.
threshold
Minimum response size in bytes to compress.
Default 1024
bytes or 1kb
.
Manually turning compression on and off
You can always enable compression by setting ctx.compress = true
.
You can always disable compression by setting ctx.compress = false
.
This bypasses the filter check.
app.use((ctx, next) => {
ctx.compress = true
ctx.body = fs.createReadStream(file)
})