initial commit
This commit is contained in:
commit
168093e034
7 changed files with 210 additions and 0 deletions
30
index.js
Normal file
30
index.js
Normal 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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue