This package is an utility tool to help generate route definitions of roe or egg
roe-define-router
makes it quite easy to define
- normal roe or egg routes
- SSR pages routes (this features requires that
app.next
exists in whichapp
is the instance of roe or egg, andapp.next
should be an instance ofnext server
) - static files serving.
$ npm i roe-define-router
app/router.js
const defineRouter = require('roe-define-router')
const routes = {
routes: {
'/say-hello': 'say.hello'
},
pages: {
'/:lang': 'index'
},
static: {
'/static': 'static'
}
}
const config = {
static: {
root: '/path/to/project'
}
}
module.exports = defineRouter(routes, config, app => {
// manually set other route definitions
})
- routes.routes? config?.routes? routes and options of
egg-define-router
- routes.pages? routes?.pages? pages and config of
egg-ssr-pages
- routes.static? routes?.static? files and options of
egg-serve-static
- extra?
Function(app, apply?)
an extra router function- app
RoeApplication | EggApplication
the server instance - apply?
Function(app)
method to applyroe-define-router
to the application.
- app
Returns a roe/egg router function which accepts app as the only one parameter.
If the extra
function only contains one parameter, the routes
will be applied to the application before invoking extra
.
module.exports = defineRouter(routes, config, app => {
// `routes` has already been applied
})
If the function contains two parameters, then the second argument apply
is the function to apply the routes
, so that we need to manually invoke apply(app)
to apply the routes what roe-define-router
defined.
module.exports = defineRouter(routes, config, (app, apply) => {
// do something with `app.router`
// Don't forget this line below:
apply(app)
// do something with `app.router`
})
MIT