Skip to content
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

Open
onexdata opened this issue Aug 17, 2018 · 5 comments
Open

Is it possible to load routes via standard JS objects? #474

onexdata opened this issue Aug 17, 2018 · 5 comments

Comments

@onexdata
Copy link

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:

  1. Loading routes via folder contents.
  2. Hot-reloading routes via object change/replacement
  3. Pragmatic modification of routes (i.e. through config files, receiving config changes via control routes, etc.)
  4. Massively simplifying route loading.
@jbielick
Copy link
Collaborator

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.

@andrewmiller1
Copy link

andrewmiller1 commented Aug 20, 2018

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')
}

@andrewmiller1
Copy link

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] }

@onexdata
Copy link
Author

Oh wow. Thanks for your hard work @andrewmiller1 . So a few questions:

  1. The Route object above can be created using that format via new Route(...)?
  2. Can the Route object above be created directly? i.e. those functions don't have special bindings or anything?
  3. Can the Route object above be use'd in Koa directly? like app.use(Route)?
  4. 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.

@onexdata
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants