Dito.js 路由器
Dito.js 是一个以 API 驱动为焦点的 声明性现代网络框架 根据Koa.js、Objection.js和Vue.js编写
2018年被麻省理工学院许可释放,后得到支持 https://lineto.com/ 页面存档备份,存于互联网档案馆
Dito.js 路由器是一个高性能,以树为主,框架可知的 HTTP 路由器,基于 出行路线,反过来 灵感来自 回声路由器.
怎么会这样?
路由器依赖于一个树结构,它大量使用常见的 前缀, 本质上是 a 前缀树。 。 。 .
使用量
import Koa from 'koa'
import Router from '@ditojs/router'
const app = new Koa()
const router = new Router()
// static route
router.get('/folders/files/bolt.gif', ctx => {
ctx.body = `this ain't no GIF!`
})
// param route
router.get('/users/:id', ctx => {
ctx.body = `requesting user ${ctx.params.id}`
})
// wildcard routes: `*` matches one segment, `**` the rest of the path
router.get('/books/*', ctx => {
ctx.body = `book: ${ctx.params.$}`
})
router.get('/files/**', ctx => {
ctx.body = `file: ${ctx.params.$$}`
})
// Handler found, methods are matched in any case
const { handler, params } = router.find('GET', '/users/233')
console.log(handler, params) // ctx => { ... } { id: '233' }
// HEAD requests fall back on GET routes
router.find('HEAD', '/users/233').handler // ctx => { ... }
// Entry not found
const result = router.find('GET', '/photos/233')
console.log(result.handler, result.status) // undefined 404
// Other methods handle the path, see the statuses in the API docs
router.find('POST', '/users/233') // { status: 501, allowed: ['GET', 'HEAD'] }
// Install router middleware
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
if (ctx.status !== 404) {
ctx.set('Allow', result.allowed.join(', '))
}
}
}
}
})
app.listen(4040, () => console.log('Koa app listening on 4040'))
选项和 API
见
路由器 API 文件
联 合 国 prefix 财务报告和审定财务报表 strict 选项、路径和状态
错过了.