Skip to content

Instantly share code, notes, and snippets.

@mindpivot
Last active March 14, 2017 19:03
Show Gist options
  • Save mindpivot/096f76cb93d6c03a08836022c30b5a75 to your computer and use it in GitHub Desktop.
Save mindpivot/096f76cb93d6c03a08836022c30b5a75 to your computer and use it in GitHub Desktop.
route tree example for koa.js and react router 4
// CLIENT IMPORTS
const InventoryNav = require('./../shared/screens/inventory/subnav');
const InventoryPage = require('./../shared/screens/inventory/page');
const Adjustments = require('./../shared/screens/inventory/adjustments');
// SERVER IMPORTS
const datarenderer = require('../server/middleware/datarenderer');
const bodyparser = require('../server/middleware/bodyparser');
const renderer = require('../server/middleware/renderer');
const InventoryRoutes = [
{
path: '/inventory',
roles: ['admin', 'user'],
client: {
enabled: true,
exact: false,
subnav: InventoryNav,
main: InventoryPage
},
server: [
{
enabled: true,
method: 'GET',
redirectTo: '/inventory/adjustments',
handlers: [bodyparser(), renderer()]
}
],
api: [
{
enabled: false
}
],
routes: [
{
path: '/inventory/adjustments',
roles: ['admin', 'user'],
client: {
enabled: true,
exact: false,
main: Adjustments
},
server: [
{
enabled: true,
method: 'get',
handlers: [bodyparser(), datarenderer(), renderer()]
}
],
api: [
{
enabled: false,
method: 'post',
handlers: [bodyparser(), datarenderer()]
}
]
}
]
}
];
export default InventoryRoutes;
@mindpivot
Copy link
Author

mindpivot commented Feb 22, 2017

So from here, for koa.js I run it through a getServerRoutes(routes); function that flattens the server (and API routes) so the shape for koa-router is routes = [{path: '...', roles: [...], method: 'get', handlers: []}, {...}, {...}] which I then iterate over and attach to the koa-router object one at a time. i can do the same for server and API routes and just drop the renderer() function.

I also pull the client object up to be peer to the pathname for React Router, and delete the server/api keys. it's a whole thing. but once those route parsers are written you load the configs by passing them through those parsing functions then pass the results to your destination, be it React Router, koa.js server, or a set of API routes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment