Refactor MIME type helper function and move outside server block
This commit is contained in:
19
server.ts
19
server.ts
@@ -1,15 +1,8 @@
|
||||
// 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) {
|
||||
switch (filename.split(".").pop()?.toLowerCase()) {
|
||||
case "js":
|
||||
return "application/javascript";
|
||||
case "ts":
|
||||
@@ -34,6 +27,12 @@ const server = Bun.serve({
|
||||
}
|
||||
}
|
||||
|
||||
const server = Bun.serve({
|
||||
port: 5173,
|
||||
async fetch(req) {
|
||||
const url = new URL(req.url);
|
||||
const pathname = url.pathname;
|
||||
|
||||
// Serve root as index.html
|
||||
if (pathname === "/")
|
||||
try {
|
||||
@@ -56,11 +55,9 @@ const server = Bun.serve({
|
||||
if (!exists)
|
||||
return new Response(`File not found: ${filename}`, { status: 404 });
|
||||
|
||||
const mimeType = getMimeType(filename);
|
||||
|
||||
return new Response(file, {
|
||||
headers: {
|
||||
"Content-Type": mimeType,
|
||||
"Content-Type": getMimeType(filename),
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user