Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
41 lines (38 loc) · 1.39 KB
/
Copy pathindex.js
File metadata and controls
41 lines (38 loc) · 1.39 KB
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
36
37
38
39
40
41
/**
* Generate an ISO String
* @returns {string} timestamp
*/
function timestamp() {
return new Date().toISOString();
}
async function main () {
// @ts-ignore
const server = Bun.serve({
// `routes` requires Bun v1.2.3+
routes: {
// Static routes
/**
* Handle all requests matching sub
* @param {Request} req
* @returns {Promise<Response>}
*/
"/*": async req => {
let url = new URL(req.url, `http://${req.headers.get("host")}`);
if (url.pathname.endsWith('/')) {
// @ts-ignore
return new Response(await Bun.file("./index.html"), { headers: { "Content-Type": "text/html" } })
} else if (url.pathname.endsWith(".css")) {
// @ts-ignore
return new Response(await Bun.file(`./${url.pathname}`), { headers: { "Content-Type": "text/css" } })
} else if (url.pathname.endsWith(".js")) {
// @ts-ignore
return new Response(await Bun.file(`./${url.pathname}`), { headers: { "Content-Type": "application/javascript" } })
} else {
return new Response("Not Found", { status: 404 });
}
},
},
});
console.log(`Server running at ${server.url}`);
}
main()