-
Notifications
You must be signed in to change notification settings - Fork 404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to load routes via standard JS objects? #474
Comments
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:
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. |
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:
The text was updated successfully, but these errors were encountered: