-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathserver.ts
More file actions
33 lines (29 loc) · 767 Bytes
/
server.ts
File metadata and controls
33 lines (29 loc) · 767 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
const server = Bun.serve({
port: 3000,
fetch: async (request) => {
console.log();
const url = new URL(request.url);
if (url.pathname === "/") {
const html = await Bun.file("index.html").text();
const response = new Response(html, {
status: 200,
headers: {
["Content-Type"]: "text/html",
},
});
return response;
}
const js = await Bun.file(url.pathname.slice(1))
.text()
.catch(() => Bun.file(url.pathname.slice(1) + ".js").text());
const response = new Response(js, {
status: 200,
headers: {
["Content-Type"]: "application/javascript",
},
});
//
return response;
},
});
console.log("Server listening on port:", server.port);