mirror of
https://github.com/rjNemo/deno_hello
synced 2026-06-06 01:56:40 +00:00
40 lines
1,015 B
TypeScript
40 lines
1,015 B
TypeScript
import { Application, Router, RouterContext } from "./deps.ts";
|
|
import { ID, sayHello, sayRandomHello } from "./src/index.ts";
|
|
import { htmlBody } from "./view.ts";
|
|
|
|
const port = 8000;
|
|
|
|
type AppOpts = { port: number };
|
|
const getApplication = ({ port }: AppOpts): Application => {
|
|
const router = new Router();
|
|
|
|
router.get("/", (ctx: RouterContext) => {
|
|
ctx.response.body = htmlBody(sayRandomHello());
|
|
}).get("/:id", (ctx: RouterContext) => {
|
|
try {
|
|
const value = ctx.params.id;
|
|
if (value) {
|
|
const id = new ID(parseInt(value, 10));
|
|
ctx.response.body = htmlBody(sayHello(id));
|
|
}
|
|
} catch (error) {
|
|
console.error(error);
|
|
ctx.response.body = error.message;
|
|
}
|
|
});
|
|
|
|
const app = new Application();
|
|
|
|
app.use(router.routes());
|
|
|
|
app.addEventListener(
|
|
"listen",
|
|
() => console.log(`Server listening on http://localhost:${port}/`),
|
|
);
|
|
return app;
|
|
};
|
|
|
|
const app = getApplication({ port });
|
|
|
|
await app.listen({ port });
|
|
console.log(`Finished`);
|