Dito.js Router
Dito.js is a declarative and modern web framework with a focus on API driven
development, based on Koa.js, Objection.js and Vue.js
Released in 2018 under the MIT license, with support by https://lineto.com/
Dito.js Router is a high performance, tree-based, framework agnostic HTTP
router, based on trek-router, which in turn
is inspired by Echo's Router.
How does it work?
The router relies on a tree structure which makes heavy use of common
prefixes, essentially a prefix tree.
Usage
import Koa from 'koa'
import Router from '@ditojs/router'
const app = new Koa()
const router = new Router()
router.get('/folders/files/bolt.gif', ctx => {
ctx.body = `this ain't no GIF!`
})
router.get('/users/:id', ctx => {
ctx.body = `requesting user ${ctx.params.id}`
})
router.get('/books/*', ctx => {
ctx.body = `sub-route: ${ctx.params['*']}`
})
let { handler, params } = router.find('get', '/users/233')
console.log(handler)
let { handler, params } = router.find('get', '/photos/233')
console.log(handler)
app.use(async (ctx, next) => {
const { method, path } = ctx
const result = router.find(method, path)
const { handler, params } = result
if (handler) {
ctx.params = params || {}
return handler(ctx, next)
} else {
try {
await next()
} finally {
if (ctx.body === undefined && ctx.status === 404) {
ctx.status = result.status || 404
if (ctx.status !== 404 && result.allowed) {
ctx.set('Allow', result.allowed.join(', '))
}
}
}
}
})
app.listen(4040, () => console.log('Koa app listening on 4040'))