koa-power-router
Still under development!
Features:
onSTATUS
handler (globally or per controller)beforeAction
: a function list that runs before router action, if any of them return values exceptundefined
ornull
will skip action, and go toonSTATUS
handler
How to use
Install:
npm install --save koa-power-router
Usage:
var koa = var app = var router = var Controller = var options = strictSlash: true // or false app router // Functions and Generators will auto-convert to Controllerrouter // Define a Controller with beforeActionvar user = { // if not login and token is empty if thisurl !== 0 && !thiscookies return true } { if thismethod === 'get' this else } { this } { this } routerrouter router
Router
Methods
router.configure(options)
approuter// equals toapp
Supported options:
strictSlash
HTTP RFC 2396 defines path separator to be single slash.
However, unless you're using some kind of URL rewriting (in which case the rewriting rules may be affected by the number of slashes), the uri maps to a path on disk, but in (most?) modern operating systems (Linux/Unix, Windows), multiple path separators in a row do not have any special meaning, so /path/to/foo and /path//to////foo would eventually map to the same file.
http://stackoverflow.com/questions/10161177/url-with-multiple-forward-slashes-does-it-break-anything
If strictSlash
is true
, URL like //demo
will not match the following router:
router
router.set(url, methods, action)
url
: the pattern of URL, convertd to regular expression with path-to-regexp.
url
supports:
- String:
'/homepage'
- Named string:
'/uesr/:id'
- Regular expression:
/^/item/(\d)+/i
methods
: the HTTP methods that this router should support, can be string
and array
.
action
: function
or generator
.
Shortcuts
router.all(url, action)
router.get(url, action)
router.post(url, action)
router.put(url, action)
router.delete(url, action)
router.on(status, handler)
Register a handler for specified status.
router
With this feature, you can render error page without redirecting to an actual error page.
Controller
An exmaple of how to define a controller:
var user = { if !thiscookies return true } { this } { }
Action
Same as the Koa middleware, nothing different.
{ this next}
Methods
on(status, handler)
Register a handler for specified status.
user
If a status is handled by a controller, router's handler will be skipped.
With this feature, you can render error page without redirecting to an actual error page.
Properties
beforeAction
A list of function that run before controller's action. For example:
ar user = { if !thiscookies thisstatus = 401 return true } { this } { this } routerrouter
Any request of /dashboard
without token
will go to on401
handler and render user/not-authorized
page instead dashboard
page.
Yieldables
Power router uses yieldr
to yield generatos and promises, you should notice that yieldr
is slightly different from co
.
If you really need some features that provided by co
, use co
directly:
var co = router
Or:
router
Contributors
Via GitHub