Files
UDN/index.ts
T

42 lines
877 B
TypeScript
Raw Normal View History

2024-07-04 16:11:02 +02:00
import Path from "path";
// CONFIG
const staticDir = "static/dist";
const defaultPage = "index.html";
const errorPage = "404.html";
// SERVER
const server = Bun.serve({
fetch(request, server) {
2024-07-04 16:18:40 +02:00
// allow websockets
server.upgrade(request);
2024-07-04 16:11:02 +02:00
2024-07-04 16:18:40 +02:00
// respond
const requestPath = new URL(request.url).pathname;
2024-07-04 16:11:02 +02:00
try {
switch (requestPath) {
case "/":
return createFileResponse(defaultPage);
default:
return createFileResponse(requestPath);
}
} catch {
return createFileResponse(errorPage);
}
},
2024-07-04 16:18:40 +02:00
websocket: {
message(ws, message) {
},
close(ws, code, reason) {},
},
2024-07-04 16:11:02 +02:00
});
function createFileResponse(requestPath: string): Response {
const fullPath = Path.join(staticDir, requestPath);
return new Response(Bun.file(fullPath));
}
console.log(`Server up on port ${server.port}.`);