Bun

指南HTTP

使用 Bun 的 Node.js 串流串流 HTTP 伺服器

在 Bun 中,Response 物件可以接受 Node.js Readable

這是因為 Bun 的 Response 物件允許任何非同步可迭代物件作為其主體。Node.js 串流是非同步可迭代物件,因此你可以將它們直接傳遞給 Response

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" },
    });
  },
});