-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
64 lines (52 loc) · 1.71 KB
/
app.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { Application, send, Context } from "https://deno.land/x/oak/mod.ts";
import { oakCors } from "https://deno.land/x/cors/mod.ts";
import { red } from "https://deno.land/std@0.53.0/fmt/colors.ts";
import router from "./routes/index.ts";
import { staticFileMiddleware } from "./middeware/staticServe.ts";
const HOST = "localhost";
const PORT = 7700;
const app = new Application();
app.use(oakCors());
app.use(staticFileMiddleware);
// Logger
app.use(async (ctx, next) => {
await next();
const rt = ctx.response.headers.get("X-Response-Time");
console.log(`${ctx.request.method}: ${ctx.request.url} - ${rt}`);
});
// Timing
app.use(async (ctx, next) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
ctx.response.headers.set("X-Response-Time", `${ms}ms`);
});
app.use(router.routes());
app.use(router.allowedMethods());
// // Send static content
// app.use(async (context: any, next) => {
// console.log(`${Deno.cwd()}${context.request.url.pathname}`);
// // if (context.request.url.pathname === "/files") {
// await send(context, `${context.request.url.pathname}`, {
// root: `${Deno.cwd()}`,
// });
// // } else {
// // await next();
// // }
// });
app.use((ctx: Context) => {
console.log(`${red("Route Not Found")}`);
ctx.response.status = 404;
ctx.response.body = { msg: "Not Found" };
});
app.addEventListener("error", (evt) => {
// Will log the thrown error to the console.
console.log(`${red("Error------------")}${red(evt.error)}`);
});
app.addEventListener("listen", () => {
console.log(`Listening on port ${HOST}:${PORT} ...`);
});
const start = async (HOST: String, PORT: Number) => {
await app.listen(`${HOST}:${PORT}`);
};
start(HOST, PORT);