# 发送响应 > H3 会自动将任何返回值转换为 web 响应。 从 [事件处理器](/guide/basics/handler) 返回的值会被 H3 自动转换为 web 的 [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response)。 **示例:** 简单事件处理函数。 ```js const handler = defineHandler((event) => ({ hello: "world" })); ``` H3 会智能转换该处理器为: ```js const handler = (event) => new Response(JSON.stringify({ hello: "world" }), { headers: { "content-type": "application/json;charset=UTF-8", }, }); ``` 🚀 H3 内部使用 srvx 的 `FastResponse` 来优化 Node.js 运行时的性能。 如果事件处理器返回一个 [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) 或来源于 [async 函数](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function),H3 会等待其完成后再发送响应。 如果抛出错误,H3 会自动用错误处理器进行处理。 ## 准备响应 在主处理器返回响应之前,可以使用 [`event.res`](/guide/api/h3event#eventres) 来准备响应头和状态。 ```js defineHandler((event) => { event.res.status = 200; event.res.statusText = "OK"; event.res.headers.set("Content-Type", "text/html"); return "

Hello, World

"; }); ``` 如果直接返回完整的 [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response) 对象,预设的状态将被丢弃,且响应头会被合并/覆盖。出于性能考虑,建议此时只在最终 Response 中设置头信息。 若发生错误,预设的状态和头信息将被丢弃。 ## 响应类型 H3 会智能将 JavaScript 值转换为 web 的 [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response)。 ### 可序列化为 JSON 的值 返回一个可通过 [JSON](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON) 序列化的值(**对象**、**数组**、**数字**或 **布尔值**)时,H3 会使用 [JSON.stringiffy()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) 进行序列化,并以默认的 `application/json` 内容类型发送。 **示例:** ```ts app.get("/", (event) => ({ hello: "world" })); ``` 返回的对象若含有 `.toJSON()` 属性,可以自定义序列化行为。详情可参考 [MDN 文档](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)。 ### 字符串 返回字符串时,内容会作为纯文本体发送。 如果未设置 `content-type` 头,默认类型为 `text/plain;charset=UTF-8`。 **示例:** 发送 HTML 响应。 ```ts app.get("/", (event) => { event.res.headers.set("Content-Type", "text/html;charset=UTF-8"); return "

hello world

"; }); ``` 你也可以使用 `html` 工具作为快捷方式。 ```js import { html } from "h3"; app.get("/", (event) => html(event, "

hello world

")); ``` ### `Response` 返回一个 web [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response) 对象时,直接将其作为最终响应发送。 **示例:** ```ts app.get( "/", (event) => new Response("Hello, world!", { headers: { "x-powered-by": "H3" } }), ); ``` 发送 `Response` 时,之前设置的任何预设头信息将被合并为默认头,且 `event.res.{status,statusText}` 会被忽略。为性能考虑,建议只在最终 `Response` 中设置头信息。 ### `ReadableStream` 或 `Readable` 返回 [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) 或 Node.js 的 [`Readable`](https://nodejs.org/api/stream.html#readable-streams) 即以流的形式发送。 ### `ArrayBuffer`、`Uint8Array` 或 `Buffer` 发送二进制数据,如 [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)、[Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) 或 Node.js [Buffer](https://nodejs.org/api/buffer.html#buffer) 对象。 `content-length` 头会被自动设置。 ### `Blob` 发送 [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) 作为流。 `Content-type` 和 `Content-Length` 头会被自动设置。 ### `File` 发送 [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File) 作为流。 `Content-type`、`Content-Length` 和 `Content-Disposition` 头会被自动设置。 ## 特殊类型 以下是响应类型中一些不太常见的可能值。 ### `null` 或 `undefined` 发送一个空响应体。 如果事件处理器中没有使用 `return` 语句,效果等同于 `return undefined`。 ### `Error` 返回一个 [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) 实例时,将发送此错误。 建议 `throw` 异常而非直接返回错误实例,这样可以在任何嵌套工具中正确传播。 ### `BigInt` 会将 BigInt 类型值转换为字符串后发送。 返回 JSON 对象时不支持 BigInt 序列化,你需要自己实现 `.toJSON`。详情可参考 [MDN 文档](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)。 ### `Symbol` 或 `Function` **返回 Symbol 或 Function 的行为不可确定。** 当前 H3 会发送类似字符串的未知 Symbol 和 Function 表示,但未来版本可能会改为抛出错误。 以下是 H3 内部已知的部分 Symbol: - `Symbol.for("h3.notFound")`:表示未找到路由,将抛出 404 错误。 - `Symbol.for("h3.handled")`:表示请求已被某种方式处理,H3 不再继续(仅 Node.js 环境)。