A lightweight deno server that runs on oak.
Run it with deno --allow-net server.ts
import { Application } from 'https://deno.land/x/oak/mod.ts' | |
const app = new Application(); | |
const handlers = { | |
// Render index.html | |
index: (ctx) => { | |
ctx.type = 'html' | |
ctx.body = html.replace(/__ASSETPATH__/g, assetPath) | |
}, | |
// Hello world! | |
greet: (ctx) => ctx.response.body = 'Hello World!', | |
// Current time | |
time: (ctx) => ctx.response.body = new Date(), | |
} | |
// 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}`) | |
}) | |
app.use(get('/time', handlers.time)) | |
app.use(get('/time', handlers.time)) | |
app.use(get('/*', handlers.index)) | |
await app.listen({ port: 8000 }) |