Is it possible to load routes via standard JS objects?
onexdata opened this issue · comments
Idea:
Loading routes pragmatically can get complex. Express router lets you load routes via JavaScript objects. I didn't see if it was possible to do this with Koa-router, is it?
Use cases:
- Loading routes via folder contents.
- Hot-reloading routes via object change/replacement
- Pragmatic modification of routes (i.e. through config files, receiving config changes via control routes, etc.)
- Massively simplifying route loading.
Those are a few cool features. I think typically those would be implemented by a framework for building applications (express broaches this territory in many ways). I don't think these features are something that would be implemented at a library-level. This library's purpose is to provide a programmatic router. Hot-reloading, recursive directory loading and registering, config files all sound like an application-building framework (or alternatively, a separate module that works with this one) to me.
You mean this?
const myRouter = ['root', '/', home]
router.get(...myRouter)
Working example (standard javascript via babel)
import Koa from 'koa'
import Router from 'koa-router'
import send from 'koa-send'
const koa = new Koa(),
router = new Router()
const port = 3000
const myRouter = ['root', '/', home]
router.get(...myRouter)
koa
.use(router.routes())
.use(router.allowedMethods())
koa.listen(port)
async function home(ctx, next) {
await send(ctx, '/src/index.html')
}
Working example (node)
const Koa = require('koa')
const Router = require('koa-router')
const send = require('koa-send')
const koa = new Koa(),
router = new Router()
const port = 3000
const myRouter = ['root', '/', home]
router.get(...myRouter)
koa
.use(router.routes())
.use(router.allowedMethods())
koa.listen(port)
async function home(ctx, next) {
await send(ctx, '/src/index.html')
}
Also, after a bit of work, you can import your own modified version of this repository that exposes Route. When you do this, you can create independent Route objects with:
import { Router, Route } from 'koa-router'
...
const myRoute = new Route({path: '/categories', method: 'get', handler: [function () {}], name: 'books'})
The resulting route:
Route {
strict: false,
sensitive: false,
method: 'get',
path: '/categories',
name: 'books',
handler: [ [Function] ],
keys: [],
regex: { /^\/categories(?:\/(?=$))?$/i keys: [] },
toPath: [Function] }
Oh wow. Thanks for your hard work @andrewmiller1 . So a few questions:
- The Route object above can be created using that format via new Route(...)?
- Can the Route object above be created directly? i.e. those functions don't have special bindings or anything?
- Can the Route object above be use'd in Koa directly? like app.use(Route)?
- Could I swap any of this out pragmatically? i.e. Koa or koa-router must maintain the route list somewhere? i.e. for hot-reloading or self-modification, can the function be swapped, or any of the above Route object be modified?
Thank you again so much for your direction. Very very helpful.
@jbielick I understand. I am just referring to pragmatic loading itself. If I had a way to have koa-router reference plain JS objects, or understood how it does it I mean, I could implement the features mentioned easily. I have done this for express because I found out how express references JS objects for routes, but I don't see the equivalent in koa-router and would like to switch my code to Koa.