-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathserver.ts
More file actions
35 lines (30 loc) · 1 KB
/
server.ts
File metadata and controls
35 lines (30 loc) · 1 KB
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
30
31
32
33
34
35
/// <reference types="@types/bun" />
import path from "node:path";
const PORT = Number(process.env.PORT ?? 3000);
const CLIENT_DIR = "./dist/client";
const SERVER_ENTRY = "./dist/server/server.js";
const serverModule = await import(SERVER_ENTRY);
const handler = serverModule.default;
const routes: Record<string, (req: Request) => Response | Promise<Response>> =
{};
const glob = new Bun.Glob("**/*");
for await (const relativePath of glob.scan({ cwd: CLIENT_DIR })) {
const filepath = path.join(CLIENT_DIR, relativePath);
const route = `/${relativePath}`;
const isHashed = /[-.][\da-f]{8,}\.\w+$/.test(relativePath);
routes[route] = () => {
return new Response(Bun.file(filepath), {
headers: isHashed
? { "Cache-Control": "public, max-age=31536000, immutable" }
: { "Cache-Control": "public, max-age=3600" },
});
};
}
Bun.serve({
port: PORT,
routes: {
...routes,
"/*": (req: Request) => handler.fetch(req),
},
});
console.log(`Server listening on port ${PORT}`);