-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.ts
More file actions
30 lines (26 loc) · 841 Bytes
/
Copy pathserver.ts
File metadata and controls
30 lines (26 loc) · 841 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
console.log("Hello via Bun!");
try {
const server = Bun.serve({
port: parseInt(process.env.PORT || "3001"),
fetch(req) {
const url = new URL(req.url);
if (url.pathname === "/") {
return new Response("Hello World!", {
headers: { "content-type": "text/html; charset=utf-8" },
});
} else if (url.pathname === "/json") {
return new Response(JSON.stringify({ hello: "world" }), {
headers: { "content-type": "application/json; charset=utf-8" },
});
}
return new Response("Not Found", {
status: 404,
headers: { "content-type": "text/plain; charset=utf-8" },
});
},
});
console.log(`Listening on http://localhost:${server.port}`);
} catch (error) {
console.error(`Failed to start server: ${error}`);
process.exit(1);
}