initial commit

This commit is contained in:
uh wot 2021-01-24 15:41:29 +01:00
commit 168093e034
Signed by: uhwot
GPG key ID: CB2454984587B781
7 changed files with 210 additions and 0 deletions

30
index.js Normal file
View file

@ -0,0 +1,30 @@
const Router = require('./router')
/**
* Example of how router can be used in an application
* */
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
function handler(request) {
const init = {
headers: { 'content-type': 'application/json' },
}
const body = JSON.stringify({ some: 'json' })
return new Response(body, init)
}
async function handleRequest(request) {
const r = new Router()
// Replace with the appropriate paths and handlers
r.get('.*/bar', () => new Response('responding for /bar'))
r.get('.*/foo', request => handler(request))
r.post('.*/foo.*', request => handler(request))
r.get('/demos/router/foo', request => fetch(request)) // return the response from the origin
r.get('/', () => new Response('Hello worker!')) // return a default message for the root route
const resp = await r.route(request)
return resp
}