init
This commit is contained in:
77
server.ts
Normal file
77
server.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
// server.ts
|
||||
|
||||
const server = Bun.serve({
|
||||
port: 5173,
|
||||
async fetch(req) {
|
||||
const url = new URL(req.url);
|
||||
const pathname = url.pathname;
|
||||
|
||||
// Helper function to get MIME type
|
||||
function getMimeType(filename: string): string {
|
||||
const ext = filename.split(".").pop()?.toLowerCase();
|
||||
switch (ext) {
|
||||
case "js":
|
||||
return "application/javascript";
|
||||
case "ts":
|
||||
return "application/javascript";
|
||||
case "css":
|
||||
return "text/css";
|
||||
case "html":
|
||||
return "text/html";
|
||||
case "json":
|
||||
return "application/json";
|
||||
case "png":
|
||||
return "image/png";
|
||||
case "jpg":
|
||||
case "jpeg":
|
||||
return "image/jpeg";
|
||||
case "svg":
|
||||
return "image/svg+xml";
|
||||
case "ico":
|
||||
return "image/x-icon";
|
||||
default:
|
||||
return "text/plain";
|
||||
}
|
||||
}
|
||||
|
||||
// Serve root as index.html
|
||||
if (pathname === "/")
|
||||
try {
|
||||
const file = Bun.file("./index.html");
|
||||
return new Response(file, {
|
||||
headers: {
|
||||
"Content-Type": "text/html",
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
return new Response("Index file not found", { status: 404 });
|
||||
}
|
||||
|
||||
// Serve other files
|
||||
const filename = pathname.startsWith("/") ? pathname.slice(1) : pathname;
|
||||
try {
|
||||
const file = Bun.file(`./${filename}`);
|
||||
const exists = await file.exists();
|
||||
|
||||
if (!exists)
|
||||
return new Response(`File not found: ${filename}`, { status: 404 });
|
||||
|
||||
const mimeType = getMimeType(filename);
|
||||
|
||||
return new Response(file, {
|
||||
headers: {
|
||||
"Content-Type": mimeType,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(`Error serving ${filename}:`, error);
|
||||
return new Response("Internal Server Error", { status: 500 });
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
console.log(`🚀 Server running at http://localhost:${server.port}`);
|
||||
console.log("📋 Available routes:");
|
||||
console.log(" - / (index.html)");
|
||||
console.log(" - /sw.js (Service Worker)");
|
||||
console.log(" - /style.css (Styles)");
|
||||
Reference in New Issue
Block a user