Kroute
Modular Koa router middleware with Express-style routes and middleware mounting.
Install
npm install kroute --save
Usage
Kroute exports a function which can be used to create router instances that work with Koa middleware.
var koa = var kroute = var app = var router = app
Initialization
A router can be initialized with default handlers based on a resourceful routing object.
Actions are mapped as follows:
GET / -> index
GET /new -> new
POST / -> create
GET /:id -> show
GET /:id/edit -> edit
PUT /:id -> update
DELETE /:id -> destroy
The object can also contain a use
property which is mounted before any routes.
Routes
Every router instance can used to attach request handlers.
router
Omitting the path name will ensure the handler is always executed when the method matches.
router
Every route method accepts multiple middleware handlers.
router
All Methods
Every router provides a .all
method for attaching request handlers to every method.
routerall {}
It accepts a path and multiple middleware like any other method.
routerall'/' {}
Mounting Middleware
Every router comes with the ability to mount middleware and handlers.
var mount = router
As long as the middleware follows the Koa generator pattern, it can be mounted.
router
Like other methods, .use
also accepts multiple request handlers and an optional path.
router
Params
Every path can be dynamic and use Express-style parameter notation.
router
Every match is stored in the params as an object.
router
The route can also be a regular expression.
router
Chaining
Every method returns it's own instance, so routes can be chained together like Express.
router
Options
Every method accepts an options object as the last argument.
router
License
MIT