Skip to content

中间件

我们把返回 Response 的原语称为“处理器(Handler)”。 “中间件”在处理器之前或之后执行,用来处理 RequestResponse, 结构就像一层层洋葱。

例如,下面的中间件可以为响应添加 “X-Response-Time” 请求头。

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
}`)
})

借助这种简单的方式,我们可以编写自定义中间件,或复用内建与第三方中间件。

Released under the MIT License.