aboutsummaryrefslogtreecommitdiff
path: root/examples/react-file-system-router/index.tsx
blob: a784c393b6239abf0e1fbeedbf905f1baf8f8796 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 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} />
      )
    )
  }
}