koa-path-router
Fast and simple routing for koa. radix-router is used to achieve fast and consistent lookups.
Installation
npm i --save koa-path-router
Usage
A minimal example
const Koa = const app = const Router = const router = // define middleware for all routes (optional) middleware: async { const startTime = Date await console } // add routes via the register functionrouter app app
Creating an instance of the router
const Router = const router =
Middleware for all routes handled by the router can be passed into the constructor
const router = // define middleware for all routes (optional) middleware: async { const startTime = Date await console }
Adding more middleware
Additional middleware can be added to subsequent routes via use
:
router
Multiple functions can be passed in if needed:
router // orrouter
Registering routes
Routes can be added by using the register
function:
// add routes via the register functionrouter
You can grab querystring values from the ctx.request
object (as usual).
router
Placeholders are added by specifying a segment with a colon (:
).
These values are placed on the ctx.request
object:
router
Wildcard routes can be added by appending a /**
to the end of a route:
router
Express style functions are exposed for convenience:
router router
Multiple middleware functions can be passed in before the final handler as well:
router // alternatively if you have a your middleware defined in an array, you can dorouter
To use the router with a koa
app instance, pass it the function returned by getRequestHandler
:
app