使用 Node.js 流的流式 HTTP 服务器
在 Bun 中,Response 接受 Node.js 的 Readable 作为其正文。
这是因为 Bun 的 Response 接受任何异步可迭代对象作为其正文,而 Node.js 流是异步可迭代对象。
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" },
});
},
});