Skip to content

Instantly share code, notes, and snippets.

@andreasvirkus
Created May 11, 2020 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreasvirkus/473a28a9b6c6268c9220546642952faa to your computer and use it in GitHub Desktop.
Save andreasvirkus/473a28a9b6c6268c9220546642952faa to your computer and use it in GitHub Desktop.
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