diff options
author | 2023-01-24 19:09:39 -0800 | |
---|---|---|
committer | 2023-01-24 19:09:39 -0800 | |
commit | ac7aff1b47b0b271d2afb010f3ffd199b9b6685d (patch) | |
tree | c6bfdae5dcca1e87fc951d06325da838f59cee4d /examples/react-file-system-router/index.tsx | |
parent | d714df48f9f47208584e6d8b91271afbcd230f70 (diff) | |
download | bun-ac7aff1b47b0b271d2afb010f3ffd199b9b6685d.tar.gz bun-ac7aff1b47b0b271d2afb010f3ffd199b9b6685d.tar.zst bun-ac7aff1b47b0b271d2afb010f3ffd199b9b6685d.zip |
Add FileSystemRouter + React example (#1894)
Diffstat (limited to 'examples/react-file-system-router/index.tsx')
-rw-r--r-- | examples/react-file-system-router/index.tsx | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/examples/react-file-system-router/index.tsx b/examples/react-file-system-router/index.tsx new file mode 100644 index 000000000..a784c393b --- /dev/null +++ b/examples/react-file-system-router/index.tsx @@ -0,0 +1,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} /> + ) + ) + } +}
\ No newline at end of file |