generated from the-road-to-deno/deno-example
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.ts
44 lines (32 loc) · 875 Bytes
/
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
41
42
43
44
import {
Application,
Router,
Context,
} from 'https://deno.land/x/oak/mod.ts';
const port = 8000;
const app = new Application();
const routerOne = new Router();
routerOne.get('/1', (ctx) => {
ctx.response.body = 'Hello Deno 1';
});
const routerTwo = new Router();
routerTwo.get('/2', (ctx) => {
ctx.response.body = 'Hello Deno 2';
});
app.use(routerOne.routes());
app.use(routerOne.allowedMethods());
app.use(routerTwo.routes());
app.use(routerTwo.allowedMethods());
const logging = async (ctx: Context, next: Function) => {
console.log(`HTTP ${ctx.request.method} on ${ctx.request.url}`);
await next();
};
app.use(logging);
app.use((ctx) => {
console.log('returning a response ...');
ctx.response.body = 'Hello Deno';
});
app.addEventListener('listen', () => {
console.log(`Listening on: localhost:${port}`);
});
await app.listen({ port });