Skip to main content
在 Bun 中,Response 对象可以接受 Node.js 的 Readable 这是因为 Bun 的 Response 对象允许任何异步可迭代对象作为其主体。Node.js 流是异步可迭代对象,因此你可以直接将它们传递给 Response
https://mintcdn.com/bun-zhcndoc/cnUTwgMuf4cCrwC-/icons/typescript.svg?fit=max&auto=format&n=cnUTwgMuf4cCrwC-&q=85&s=e7767043c9e885c34f2d6c8fe2a95217server.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" },
    });
  },
});