-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d5fcf0
commit 3ec40b0
Showing
1 changed file
with
32 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,35 @@ | ||
const path = require('path'); | ||
|
||
const BASE_PATH = path.resolve(__dirname, "./"); | ||
const BASE_PATH = "./"; | ||
|
||
Bun.serve({ | ||
port: 3000, | ||
async fetch(req) { | ||
const url = new URL(req.url); | ||
let filePath; | ||
|
||
console.log("Requested URL:", url.pathname); // Log the requested URL path | ||
|
||
// If the request is for "/", serve the "index.html" file | ||
if (url.pathname === "/") { | ||
filePath = path.join(BASE_PATH, "index.html"); | ||
} else if (url.pathname.startsWith("/static")) { | ||
// If the request starts with "/static", serve files from the "/static" directory | ||
filePath = path.join(BASE_PATH, url.pathname.replace(/^\/static/, "/static")); | ||
} else { | ||
// For all other routes, serve files directly from the root directory | ||
filePath = path.join(BASE_PATH, url.pathname); | ||
} | ||
|
||
console.log("Resolved file path:", filePath); // Log the resolved file path | ||
|
||
try { | ||
const file = await Bun.file(filePath); | ||
return new Response(file); | ||
} catch (error) { | ||
console.error("Error:", error); // Log any errors that occur | ||
|
||
// If error and the request is for a non-static route, serve index.html | ||
if (url.pathname !== "/" && !url.pathname.startsWith("/static")) { | ||
let indexPath = path.join(BASE_PATH, "index.html"); | ||
const indexFile = await Bun.file(indexPath); | ||
return new Response(indexFile); | ||
} | ||
|
||
// Otherwise, return a 404 response | ||
return new Response("404 - Not Found", { status: 404 }); | ||
port: 3000, | ||
async fetch(req) { | ||
const url = new URL(req.url); | ||
let filePath = BASE_PATH + url.pathname; | ||
|
||
console.log("Requested URL:", url.pathname); | ||
console.log("Resolved file path:", filePath); | ||
|
||
// If the request is for "/", serve the "index.html" file | ||
if (url.pathname === "/") { | ||
filePath = BASE_PATH + "/index.html"; | ||
} | ||
|
||
try { | ||
const file = Bun.file(filePath); | ||
return new Response(file); | ||
} catch (error) { | ||
console.error("Error occurred while fetching file:", error); | ||
// If error, route to index.html | ||
let errorFilePath = BASE_PATH + "/index.html"; | ||
const errorFile = Bun.file(errorFilePath); | ||
return new Response(errorFile); | ||
} | ||
}, | ||
error(error) { | ||
console.error("Server error:", error); | ||
// If error, route to index.html | ||
let filePath = BASE_PATH + "/index.html"; | ||
const file = Bun.file(filePath); | ||
return new Response(file); | ||
} | ||
} | ||
}); | ||
}); |