-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.tsx
29 lines (24 loc) · 1008 Bytes
/
server.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { renderToReadableStream } from 'react-dom/server';
import ServerPage from './server-page.tsx';
const BASE_PATH = Bun.env.BASE_PATH;
const RELATIVE_PATH = Bun.env.RELATIVE_PATH;
const PORT = Bun.env.PORT || 3000;
console.log(`Server started at port: ${PORT}`);
Bun.serve({
port: Number(PORT),
async fetch(req) {
const url = new URL(req.url);
const path = RELATIVE_PATH ? url?.pathname.replace(RELATIVE_PATH, '') : url?.pathname;
const file = Bun.file((BASE_PATH || '.') + decodeURI(path));
console.log(
`${new Date().toISOString()} - ${req.method}:: ${path} ${JSON.stringify(url.searchParams)}`
);
//File is forced to be in the specified BASE_PATH or in the current working directory.
if (await file.exists()) {
return new Response(file);
}
return new Response(
await renderToReadableStream(<ServerPage {...(Object.fromEntries(url.searchParams) as any)} />)
);
},
});