Skip to content

Commit

Permalink
feat: initial demo code
Browse files Browse the repository at this point in the history
  • Loading branch information
LePichu committed Apr 24, 2023
0 parents commit 42516f4
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.vscode
.idea

deno.lock
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Basic FS Routing with Deno and Hono
---
This is a Demo demonstrating how to use the [Hono](https://hono.dev/) to make a basic SSR (Server Side Rendering) application with Deno and Preact, tho the same technique can be applied to any framework, may or may not require a buildstep.

## How to run
Run `deno run --allow-net --allow-read --unstable server.ts` to start the server, then navigate to `localhost:3000` to see the app.

## License
This demo is licensed under [MIT License](https://choosealicense.com/licenses/mit/) with the holder "LePichu".
19 changes: 19 additions & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "https://esm.sh/[email protected]"
},
"fmt": {
"options": {
"indentWidth": 4,
"lineWidth": 80,
"semiColons": false,
"singleQuote": false,
"useTabs": true
}
},
"imports": {
"hono": "https://deno.land/x/[email protected]/mod.ts",
"preact-render-to-string": "https://esm.sh/[email protected]"
}
}
36 changes: 36 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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/"),
})
7 changes: 7 additions & 0 deletions routes/another_route.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function() {
return (
<div>
<h1>Another Route</h1>
</div>
)
}
5 changes: 5 additions & 0 deletions routes/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default function() {
return (
<h1>Hello World!</h1>
)
}

0 comments on commit 42516f4

Please sign in to comment.