aboutsummaryrefslogtreecommitdiff
path: root/examples/http-request-body.ts
blob: b9a45488965ccb7d8a48cda1c691e10637eb90ca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { serve } from "bun";

serve({
  async fetch(req) {
    // body is a ReadableStream
    const body = req.body;

    const writer = Bun.file(`upload.${Date.now()}.txt`).writer();
    for await (const chunk of body!) {
      writer.write(chunk);
    }
    const wrote = await writer.end();

    // @ts-ignore
    return Response.json({ wrote, type: req.headers.get("Content-Type") });
  },
});