运行时包管理器打包器测试运行器代码检查格式化指南参考博客反馈

使用 Node.js 流的流式 HTTP 服务器

在 Bun 中,Response 接受 Node.js 的 Readable 作为其正文。

这是因为 Bun 的 Response 接受任何异步可迭代对象作为其正文,而 Node.js 流是异步可迭代对象。

server.ts
import { Readable } from "stream";
import { serve } from "bun";
serve({
  port: 3000,
  fetch(req) {
    return new Response(Readable.from(["Hello, ", "world!"]), {
      headers: { "Content-Type": "text/plain" },
    });
  },
});