-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
35 lines (29 loc) · 831 Bytes
/
server.js
File metadata and controls
35 lines (29 loc) · 831 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Simple Bun server
import { serve } from "bun";
const port = process.env.PORT || 3001;
const entrypoint = process.env.ENTRYPOINT || "/index.html";
serve({
port: port,
async fetch(req) {
const url = new URL(req.url);
let path = url.pathname;
// Serve the entrypoint for root requests
if (path === "/") {
path = entrypoint;
}
// Handle favicon requests gracefully
if (path === "/favicon.ico") {
return new Response(null, { status: 204 });
}
// Try to serve the file from the file system (only if it exists)
const file = Bun.file(`.${path}`);
if (await file.exists()) {
return new Response(file);
}
return new Response("Not found", { status: 404 });
},
});
console.log(`Server running at http://localhost:${port}`);
if (entrypoint) {
console.log(`Serving ${entrypoint} at the root.`);
}