Merge pull request #10594 from supabase/improve-oak-example

fix: improve oak server example to add routes
This commit is contained in:
Lakshan Perera authored and GitHub committed 2022-11-29 17:09:16 +11:00
commit f6257e3ad8
1 file changed
+21 -9
@@ -1,13 +1,25 @@
import { Application } from 'https://deno.land/x/oak/mod.ts'
import { Application, Router } from 'https://deno.land/x/oak/mod.ts'
const router = new Router()
router
// Note: path will be prefixed with function name
.get('/oak-server', (context) => {
context.response.body = 'This is an example Oak server running on Edge Functions!'
})
.post('/oak-server/greet', (context) => {
// Note: request body will be streamed to the function as chunks, set limit to 0 to fully read it.
const result = context.request.body({ type: 'json', limit: 0 })
const body = await result.value
const name = body.name || 'you'
context.response.body = { msg: `Hey ${name}!` }
})
.get('/oak-server/redirect', (context) => {
context.response.redirect('https://www.example.com')
})
const app = new Application()
app.use(async (ctx) => {
const result = ctx.request.body({ type: 'json', limit: 0 })
const body = await result.value
const name = body.name || 'you'
ctx.response.body = { msg: `Hey ${name}!` }
})
app.use(router.routes())
app.use(router.allowedMethods())
await app.listen({ port: 8000 })