aboutsummaryrefslogtreecommitdiff
path: root/examples/react-file-system-router/index.tsx
blob: 2abcc6b7e4f6cfc5209764e1fc97c0b74fbe148e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// A simple way to connect FileSystemRouter to Bun#serve
// run with `bun run index.tsx`

import { renderToReadableStream } from "react-dom/server";
import { FileSystemRouter } from "bun";

export default {
  port: 3000,
  async fetch(request: Request) {
    const router = new FileSystemRouter({
      dir: process.cwd() + "/pages",
      style: "nextjs",
    });

    const route = router.match(request);

    const { default: Root } = await import(route.filePath!);
    return new Response(await renderToReadableStream(<Root {...route.params} />));
  },
};