Skip to content

中间件

¥Middleware

我们将返回 Response 的原语称为 "处理程序"。"中间件" 在 Handler 之前和之后执行,并处理 RequestResponse。它就像一个洋葱结构。

¥We call the primitive that returns Response as "Handler". "Middleware" is executed before and after the Handler and handles the Request and Response. It's like an onion structure.

Onion

例如,我们可以编写中间件来添加 "X-Response-Time" 标头,如下所示。

¥For example, we can write the middleware to add the "X-Response-Time" header as follows.

ts
app
.
use
(async (
c
,
next
) => {
const
start
=
Date
.
now
()
await
next
()
const
end
=
Date
.
now
()
c
.
res
.
headers
.
set
('X-Response-Time', `${
end
-
start
}`)
})

使用这种简单的方法,我们可以编写自己的自定义中间件,并且可以使用内置或第三方中间件。

¥With this simple method, we can write our own custom middleware and we can use the built-in or third party middleware.

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