Bun Streaming HTTP Server with Node.js Streams

Streaming HTTP Server with Node.js Streams

jarle Updated

File contents

Streaming HTTP Server with Node.js Streams

In Bun, Response objects can accept a Node.js Readable.

This works because Bun's Response object allows any async iterable as its body. Node.js streams are async iterables, so you can pass them directly to 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" },
    });
  },
});

jarle/bun-skills/tree/main/skills/bun-guides-http-stream-node-streams-in-bun commit 879d6f2f6c

Frequently asked questions

npx skillmds@latest add jarle/bun-streaming-http-server-with-node-js-streams