Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A lightweight Deno server using Oak.

Deno server

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 })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.