finish file server

This commit is contained in:
marlonerler
2024-07-04 16:11:02 +02:00
parent 8fa8bc6ed0
commit c97e3b5450
829 changed files with 143867 additions and 0 deletions
+31
View File
@@ -0,0 +1,31 @@
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) {
const requestPath = new URL(request.url).pathname;
try {
switch (requestPath) {
case "/":
return createFileResponse(defaultPage);
default:
return createFileResponse(requestPath);
}
} catch {
return createFileResponse(errorPage);
}
},
});
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}.`);