koa-simple-router
Simple and fast router for koa 2.x
Features
- support prefix
- support auto OPTIONS and 405 response
- use
path-to-regexp
to parse url - use express style routing (
.get
,.put
,.post
,.all
, etc ) - use loop to iterate through multiple routes instead of recursive calls
- better performance
- prevent max call stack error with large number of routes
Installation
$ npm install koa-simple-router
Usage
const Koa = // koa 2.xconst router = let app = app
API
router(init)
Create a router middleware with init function.
const Koa = const router = const app = app
router(options, init)
Create a router middleware with options and init function.
Default options is the same as path-to-regexp
.
- prefix (default:
null
) - sensitive (default:
false
) - strict (default:
false
) - end (default:
true
)
const Koa = const router = const app = app
_.verb(path, ...mw)
app
_.all(path, ...[mw | obj])
Middleware mode: works just like _.verb(path, ...mw)
but ignore ctx.method
app
Object mode: accept an object with method as key and middleware or array of middleware as value
- auto
HEAD
response ifGET
present - auto
OPTIONS
response withAllow
header - auto 405 response with
Allow
header
app
_.param(param, ...mw)
Register middleware for named route parameters.
app
License
MIT