请求生命周期

¥Request Lifecycle

H3 将传入的 Web 请求分派到最终的 Web 响应。

¥H3 dispatches incoming web requests to final web responses.

以下是 H3 服务器从 HTTP 请求到达到生成响应的整个过程的概述。

¥Below is an overview of what happens in a H3 server from when an HTTP request arrives until a response is generated.

1. 传入请求

¥ Incoming Request

当浏览器或 fetch() 发送 HTTP 请求时,服务器获取处理程序会收到一个 请求 对象。

¥When An HTTP request is made by Browser or fetch(), server fetch handler receives a Request object.

💥 Srvx 提供统一的 server.fetch 接口,并添加了 Node.js 兼容性。¥!TIP💥 Srvx provides unified server.fetch interface and adds Node.js compatibility.

2. 接受请求

¥ Accept Request

H3 根据传入的请求初始化 H3Event 实例,调用 onRequest 全局钩子,最后使用初始化的事件调用 H3.handler

¥H3 Initializes an H3Event instance from incoming request, calls onRequest global hook and finally H3.handler with the initialized event.

3. 调度请求

¥ Dispatch Request

H3 匹配路由 基于 request.urlrequest.method,调用全局 中间件,最后将路由处理程序函数与事件匹配。

¥H3 matches route based on request.url and request.method, calls global middleware and finally matched route handler function with event.

🚀 H3 内部使用 srvx FastURL 而不是 new URL(req.url).pathname。¥!TIP 🚀 Internally, H3 uses srvx FastURL instead of new URL(req.url).pathname.

4. 发送响应

¥ Send Response

H3 将 converts 返回的值和 准备好的标头 合并到 响应 中,调用 onResponse 全局钩子,最后将响应返回给服务器的获取处理程序。

¥H3 converts returned value and prepared headers into a Response, calls onResponse global hook and finally returns response back to the server fetch handler.