Skip to content

压缩中间件

¥Compress Middleware

此中间件根据 Accept-Encoding 请求标头压缩响应主体。

¥This middleware compresses the response body, according to Accept-Encoding request header.

信息

注意:在 Cloudflare Workers 和 Deno Deploy 上,响应主体将自动压缩,因此无需使用此中间件。

¥Note: On Cloudflare Workers and Deno Deploy, the response body will be compressed automatically, so there is no need to use this middleware.

Bun:此中间件使用 bun 中尚不支持的 CompressionStream

¥Bun: This middleware uses CompressionStream which is not yet supported in bun.

导入

¥Import

ts
import { Hono } from 'hono'
import { compress } from 'hono/compress'

用法

¥Usage

ts
const app = new Hono()

app.use(compress())

选项

¥Options

optional 编码:'gzip' | 'deflate'

¥optional encoding: 'gzip' | 'deflate'

允许响应压缩的压缩方案。gzipdeflate。如果未定义,则两者都允许,并将基于 Accept-Encoding 标头使用。如果未提供此选项且客户端在 Accept-Encoding 标头中同时提供两者,则优先考虑 gzip

¥The compression scheme to allow for response compression. Either gzip or deflate. If not defined, both are allowed and will be used based on the Accept-Encoding header. gzip is prioritized if this option is not provided and the client provides both in the Accept-Encoding header.

<徽章类型="info" 文本="optional" /> 阈值:number

¥optional threshold: number

要压缩的最小大小(以字节为单位)。默认为 1024 字节。

¥The minimum size in bytes to compress. Defaults to 1024 bytes.

Hono v4.7 中文网 - 粤ICP备13048890号