mirror of
https://github.com/rjNemo/deno_hello
synced 2026-06-12 12:56:38 +00:00
html frontend
This commit is contained in:
parent
0f54f17f18
commit
b42c701f20
2 changed files with 32 additions and 19 deletions
1
deps.ts
1
deps.ts
|
|
@ -6,3 +6,4 @@ export {
|
||||||
} from "https://deno.land/std@0.87.0/testing/asserts.ts";
|
} from "https://deno.land/std@0.87.0/testing/asserts.ts";
|
||||||
export { readLines } from "https://deno.land/std@0.87.0/io/bufio.ts";
|
export { readLines } from "https://deno.land/std@0.87.0/io/bufio.ts";
|
||||||
export { Application, Router } from "https://deno.land/x/oak/mod.ts";
|
export { Application, Router } from "https://deno.land/x/oak/mod.ts";
|
||||||
|
export type { RouterContext } from "https://deno.land/x/oak/mod.ts";
|
||||||
|
|
|
||||||
50
server.ts
50
server.ts
|
|
@ -1,29 +1,41 @@
|
||||||
import { Application, Router } from "./deps.ts";
|
import { Application, Router, RouterContext } from "./deps.ts";
|
||||||
import { ID, sayHello, sayRandomHello } from "./src/index.ts";
|
import { ID, sayHello, sayRandomHello } from "./src/index.ts";
|
||||||
|
|
||||||
const port = 8000;
|
const port = 8000;
|
||||||
|
|
||||||
const router = new Router();
|
const htmlBody = (content: string) =>
|
||||||
|
`<!DOCTYPE html><html><body><pre>${content}</pre></body></html>`;
|
||||||
|
|
||||||
router.get("/", (ctx: any) => {
|
type AppOpts = { port: number };
|
||||||
ctx.response.body = JSON.stringify(sayRandomHello());
|
const get_application = ({ port }: AppOpts): Application => {
|
||||||
}).get<{ id: string }>("/:id", (ctx: any) => {
|
const router = new Router();
|
||||||
try {
|
|
||||||
const value = ctx.params.id;
|
|
||||||
const id = new ID(value);
|
|
||||||
ctx.response.body = JSON.stringify(sayHello(id));
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
ctx.response.body = error.message;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const app = new Application();
|
router.get("/", (ctx: RouterContext) => {
|
||||||
app.use(router.routes());
|
ctx.response.body = htmlBody(JSON.stringify(sayRandomHello(), null, 2));
|
||||||
|
}).get("/:id", (ctx: RouterContext) => {
|
||||||
|
try {
|
||||||
|
const value = ctx.params.id;
|
||||||
|
if (!!value) {
|
||||||
|
const id = new ID(parseInt(value, 10));
|
||||||
|
ctx.response.body = htmlBody(JSON.stringify(sayHello(id), null, 2));
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
ctx.response.body = error.message;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
app.addEventListener("listen", ({ port }: any) => {
|
const app = new Application();
|
||||||
console.log(`Server listening on http://localhost:${port}/`);
|
|
||||||
});
|
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 });
|
await app.listen({ port });
|
||||||
console.log(`Finished`);
|
console.log(`Finished`);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue