Skip to content

Commit

Permalink
chore: troubleshooting
Browse files Browse the repository at this point in the history
  • Loading branch information
devops-chris committed Feb 27, 2024
1 parent 8d5fcf0 commit 3ec40b0
Showing 1 changed file with 32 additions and 40 deletions.
72 changes: 32 additions & 40 deletions frontend/index.js
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);
}
}
});
});

0 comments on commit 3ec40b0

Please sign in to comment.