-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathserver.ts
40 lines (34 loc) · 1.16 KB
/
server.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
import { Application, HttpError, Status } from "https://deno.land/x/oak/mod.ts";
const app = new Application();
// Error Handling
app.use(async (context, next) => {
try {
await next();
} catch (e) {
if (e instanceof HttpError) {
context.response.status = e.status as any;
if (e.expose) {
context.response.body = `<!doctype html><html><body><h1>${e.status} - ${e.message}</h1></body></html>`;
} else {
context.response.body = `<!doctype html><html><body><h1>${e.status} - ${Status[e.status]}</h1></body></html>`;
}
} else if (e instanceof Error) {
context.response.status = 500;
context.response.body = `<!doctype html><html><body><h1>500 - Internal Server Error</h1></body></html>`;
console.log("Unhandled Error:", e.message);
console.log(e.stack);
}
}
});
//Static serving
app.use(async (ctx) => {
await ctx.send({
root: `${Deno.cwd()}/gh-pages/`,
index: 'index.html',
});
});
app.addEventListener('listen', ({hostname, port}) => {
console.log(`Serving ${Deno.cwd()}`);
console.log(`Start listening on ${hostname}:${port}`);
})
await app.listen({hostname: "0.0.0.0", port: 8000 });