Rainbow
A node Express router middleware for RESTful API base on certain folder path.
Rainbow mapping all HTTP request route to controllers folder each as path to file as URL.
Installation
$ npm install rainbow
Usage
In your express application main file app.js
:
var express = ;var rainbow = ; var app = ; // Here using Rainbow to initialize all routersapp; app;
Controllers
All your controllers for catching HTTP request should be defined in each file in controllers/
folder (could be changed) as same path in URL.
This is the core design for Rainbow! And it makes routing much simpler only by files' paths!
Here writes a router something.js
in your controllers/
folder like this:
exports { res;};
If you need some filters, just use an array (RECOMMENDED), or add a filters
array property which contains your filters each as a function to the handle function like this:
var authorization = ; exportsGET = authorization { res;}; // orexports { res;};// add filtersexportsGETfilters = authorization;
Also you could define other HTTP methods handlers, but make sure in one file each URL! Example in controllers/user.js
:
exports { User;}; exports { User;}; // You can also define `post` and `delete` handlers.// ...
If you want all methods to be process in only one controller(something not RESTful), just make exports to be the handle function:
module { // all your process};
Params
Params config is supported via router key definition (RECOMMENDED) from v2.0.0. Now you can define express-like routers as a form of <method> <params>
. Named params form as in express could be same like :name
, while regular expression form should also use string type and wrapped with /
at beginning and end. Here are examples:
// normal router without params// will match when GET with no paramsexports {}; // router with named params// will match when GET /:usernameexports'GET :username' = { res;}; // router with regular expression params// will match when GET /123/profileexports'GET /(\\d+)/profile/' = { res;};
Params config is also supported via .params
form URL from v0.1.0. You can define your controllers URL with params resolved by native Express like this:
exports { var id = reqparamsid; // your business}; exportsGETparams = ':id?';
Or you can use regular expression also:
exports { console;} exportsGETparams = /?/;
But make sure no regular expression ^
used as starter and $
as ender, or rainbow could not resolve the expression correctly. And be aware of the tailing slash when using .params
definition, the GET /resource
will be not found, while GET /resources/
would be match.
Filters
Filter is as same as a origin middleware in Express. Define an action with filters by using .filters
property as an array. Here authorization.js
is a example for intecepting by non-authenticated user before GET
http://yourapp:6060/something
:
module { console; var session = reqsession; if sessionuserId console; ; else console; // Async filter is ok with express! dbUser; };
Filters only support function from v1.0.0.
// controller file test.js route to [GET]/test { // blablabla... ;} exports { // blablabla...}; exportsGETfilters = myFilter;
If you need some filters to be applied for all methods in an URL, you could use URL level filters definition:
// controller file test.js route to [GET|POST]/testexports {};exports {};exportsPOSTfilters = validation;exportsfilters = session;
When user GET:/test
the filter session
would run, and when POST:/test
URL level filter session
run first and then validation
.
Options
Change default path
Controllers default path could be changed by passing a path config object to route
function when initializing:
app;
Path option here supports both RELATIVE and ABSOLUTE path. The relative path will be calculated base on your app.js
file.
Glob
From v2.1.0 you could use glob
in options to config any option supported by npm glob, such as excluding files.
app;
Express router options
From v2.3.0 rainbow added express router option strict
into options, default to false
.
app;
Change log
2.2.0
- Add both absolute and relative controller path configuration support.
2.1.1
- Fix #6: chaining express application requiring controller path issue caused by node module cache. So that rainbow cannot be cached when using multi requiring.
2.1.0
- Add
glob
option for more glob configurations.
2.0.0
- Parameters definition supports key form with HTTP method (e.g.:
GET :id
). - Option of controllers path changed from absolute to relative.
1.0.0
- Main API changed from
rainbow.route(app)
toapp.use(rainbow())
. - Add
function
type support for filters definition. - Remove
string
named filters definition. Also remove the filters path option in config. - Add
Array
type support for router definition. - Remove
.coffee
support. - Add test cases.
Troubleshooting
- Gmail me: mytharcher
- Write a issue
- Send your pull request to me.
MIT Licensed
-EOF-