koa-66
Router middleware for koa v2.
feedbacks are welcome
Features
- Plugin middleware support
- Express like http verbs methods (including
all
) - Express like use function
- Express like param function
- Automatic OPTIONS response
- Automatic HEAD when GET is present
- 501 and 405 status (throw capability with headers)
- Mount instance on specific path
- Multiple middleware as arguments
- Multiple middleware as array
Installation
# with npm $ npm install koa-66 $ with yarn$ yarn add koa-66
Example
const Koa = ;const Router = ;const app = ; const router = ;const mainRouter = ; router; router; router; router; mainRouter; app; app;// GET http://localhost:1664/pouet/world// => hello world
Example with throw option
const Koa = ;const Router = ;const app = ; const router = ; app router; app; app; // > curl http://localhost:1664/ -I -X POST//// HTTP/1.1 405 Method Not Allowed// allow: HEAD, GET// Content-Type: text/plain; charset=utf-8// Content-Length: 18// Date: Wed, 04 Nov 2015 10:29:06 GMT// Connection: keep-alive
Plugin support
I don't know if Plugin is a good term for this feature. The goal was to add cappability to register some middleware on a main Router that will be inject via config object on different route. (ex: authentication or acl behaviour). Why? Because I am lazy to require some middleware in all my router script with generaly relatif path...
So I decided to add the possibility to inject an object at first parameter (that will be a config object) and adding an extra middleware that will be inject in middleware stack. To register this plugin just use a plugin()
method.
const Router = ;const main = ; // you can use multiple middleware as arguments or arraymain main const router = ; router;//options here is a boolean,//but you can pass everything you want,//and it will be inject as options router main;... // order of call /api/private// 1 plugin authent// 2 plugin acl// 3 real middleware
Test
# npm test