Skip to content

中间件

🌐 Middleware

我们将返回 Response 的原语称为“处理器”。 “中间件”在处理器执行的前后运行,并处理 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.

例如,我们可以编写中间件来添加“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
=
performance
.
now
()
await
next
()
const
end
=
performance
.
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 中文网 - 粤ICP备13048890号