veins
A koa router middleware for RESTful API basing on the folder path.
Venis mapping all paths inside the specified folder to HTTP request routes.
Installation
npm install veins --save
Usage
In your app.js
:
let koa = ;let veins = ; let app = ; veins;//Then venis will initialize all routers. app;
Controllers & filters
Venis will mount all js
files in ./controllers
(could be changed) to your app's router.
For example, you have a file named user.js
in folder ./controllers
:
//./controllers/user.js :exports { //.....}; exports { //....};
And after calling veins.route(app);
, this file will be mounted on:
GET /userDELETE /user
Nested folders are also be supported:
//./controllers/human/man/DavidCai.js :exports { //.....};
POST /human/man/DavidCai
If you need some filters for your routers, just add a file to './fitters', and add a array property named filters
to the handle function or the exports
object in a controller.
For example, here is a file named auth.js
in ./filters
:
//./filters/auth.js :module { //.... next;}
You can require this filter in a controller:
//./controllers/user.js :exports { //.....}; //add method level filters.exportsgetfilters = 'auth'; exports { //....}; //add controller level filters.exportsfilters = 'auth';
You can add as many filters as you want!
Params
You can also define a URL with params:
//./controllers/user.js :exports { console; //.....}; exportsgetparams = ':id';
GET /user/0
All param syntax
which path-to-regexp support can be used.
Use customized path
The paths of controllers and filters can be customized by passing a config object as the second parameter to veins.route();
:
veins;
License
MIT