Skip to main content
This snippet reads a file from disk using Bun.file(). This returns a BunFile instance, which can be passed directly into the new Response constructor.
https://mintcdn.com/bun-1dd33a4e-claude-bun-git-module/aYd6IH1DX0gD-k_I/icons/typescript.svg?fit=max&auto=format&n=aYd6IH1DX0gD-k_I&q=85&s=49fc2d2e9587ce2222ea6cc2b60db513server.ts
const path = "/path/to/file.txt";
const file = Bun.file(path);
const resp = new Response(file);

The Content-Type is read from the file and automatically set on the Response.
https://mintcdn.com/bun-1dd33a4e-claude-bun-git-module/aYd6IH1DX0gD-k_I/icons/typescript.svg?fit=max&auto=format&n=aYd6IH1DX0gD-k_I&q=85&s=49fc2d2e9587ce2222ea6cc2b60db513server.ts
new Response(Bun.file("./package.json")).headers.get("Content-Type");
// => application/json;charset=utf-8

new Response(Bun.file("./test.txt")).headers.get("Content-Type");
// => text/plain;charset=utf-8

new Response(Bun.file("./index.tsx")).headers.get("Content-Type");
// => text/javascript;charset=utf-8

new Response(Bun.file("./img.png")).headers.get("Content-Type");
// => image/png

Putting it all together with Bun.serve().
https://mintcdn.com/bun-1dd33a4e-claude-bun-git-module/aYd6IH1DX0gD-k_I/icons/typescript.svg?fit=max&auto=format&n=aYd6IH1DX0gD-k_I&q=85&s=49fc2d2e9587ce2222ea6cc2b60db513server.ts
// static file server
Bun.serve({
  async fetch(req) {
    const path = new URL(req.url).pathname;
    const file = Bun.file(path);
    return new Response(file);
  },
});

See Docs > API > File I/O for complete documentation of Bun.write().