deno_hello/server.ts
2021-03-04 22:02:31 +01:00

39 lines
1 KiB
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 get_application = ({ 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", ({ port }) => {
console.log(`Server listening on http://localhost:${port}/`);
});
return app;
};
const app = get_application({ port });
await app.listen({ port });
console.log(`Finished`);