-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
44 lines (34 loc) · 1.08 KB
/
index.ts
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
42
43
44
import { Headers, CompressionTypes, RequestMethod, ContentTypes } from "./constants.ts";
import Router from './router.ts';
import HomeController from "./home-controller.ts";
import SearchController from "./search-controller.ts";
const DOMAIN = 'localhost'
const PORT = 8000;
// Router
const router = new Router();
router.Get("/", HomeController);
router.Get("/search", SearchController);
const server = Bun.serve({
port: PORT,
async fetch(request: Request) {
const path = new URL(request.url)?.pathname;
// Serve any files within a "static" directory
if (path.lastIndexOf("static/") !== -1) {
const file = Bun.file("." + path);
return new Response(file);
}
const match = router.getMatch(path);
if (match && match.handler) {
return match.handler(request);
}
throw new Error("404 - Not Found");
},
error(error) {
return new Response(`<pre>${error}\n${error.stack}</pre>`, {
headers: {
[Headers.ContentType]: ContentTypes.Html,
},
});
}
});
console.log(`Listening on http://${DOMAIN}:${server.port}`);