-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
36 lines (30 loc) · 914 Bytes
/
main.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
import { Hono } from "hono"
import { serve } from "https://deno.land/[email protected]/http/server.ts"
import { renderToString } from "preact-render-to-string"
const app = new Hono()
const map = new Map()
const files = Deno.readDirSync("./routes")
for (const file of files) {
if (file.isFile && file.name.endsWith(".tsx")) {
if (file.name === "index.tsx") {
map.set("/", `./routes/${file.name}`)
} else {
map.set(`/${file.name.replace(".tsx", "")}`, `./routes/${file.name}`)
}
}
}
app.get("*", async (ctx) => {
if(map.has(ctx.req.path)) {
const module = await import(map.get(ctx.req.path))
if(module.default) return ctx.html(`
<body>
${renderToString(module.default())}
</body>
`)
}
return ctx.html("<h1>Meow.</h1>")
})
serve(app.fetch, {
port: 8000,
onListen: () => console.log("Listening on http://localhost:8000/"),
})