Comparing version
66
index.js
@@ -1,2 +0,2 @@ | ||
var approot = process.env.PWD; | ||
var APP_ROOT = process.env.PWD; | ||
@@ -7,2 +7,3 @@ var path = require('path'); | ||
var methods = require('methods'); | ||
var express = require('express'); | ||
@@ -20,43 +21,34 @@ function joinParam (url, param) { | ||
function route (url, method, instance) { | ||
var router = instance[method.toLowerCase()] || instance[method.toUpperCase()]; | ||
if (router) { | ||
var filters = (instance.filters || []).concat(router.filters || []); | ||
var chain = filters.concat(router instanceof Array ? router : [router]); | ||
var m = method.toLowerCase(); | ||
if (typeof this[m] != 'function') { | ||
console.error('[rainbow]: http method "' + method + '" is not supported.'); | ||
return; | ||
} | ||
this[m].apply(this, | ||
[joinParam(url, router.params)].concat(chain) | ||
); | ||
} | ||
} | ||
/** | ||
* Main function to initialize routers of a Express app. | ||
* | ||
* @param {Express} app Express app instance | ||
* @param {Object} paths (optional) For configure relative paths of | ||
* controllers and filters rather than defaults. | ||
*/ | ||
exports.route = function (app, paths) { | ||
function route (url, method, instance) { | ||
var router = instance[method.toLowerCase()] || instance[method.toUpperCase()]; | ||
if (router) { | ||
var filters = (instance.filters || []).concat(router.filters || []).map(function (item) { | ||
switch (typeof item) { | ||
case 'function': | ||
return item; | ||
case 'string': | ||
return require(path.join(fltrDir, item)); | ||
default: | ||
console.error('[rainbow]: Filter only support function or string of path.'); | ||
return null; | ||
} | ||
}).filter(function (item) { | ||
return !!item; | ||
}); | ||
app[method.toLowerCase()].apply(app, [joinParam(url, router.params)] | ||
.concat(filters) | ||
.concat([router]) | ||
); | ||
} | ||
} | ||
module.exports = function (options = {}) { | ||
var router = express.Router(); | ||
var ctrlDir = options.controllers || path.join(APP_ROOT, 'controllers'); | ||
paths = paths || {}; | ||
var ctrlDir = path.join(approot, (paths.controllers || 'controllers')); | ||
var fltrDir = path.join(approot, (paths.filters || 'filters')); | ||
glob.sync(ctrlDir + "/**/*.+(coffee|js)").forEach(function (file) { | ||
glob.sync(ctrlDir + "/**/*.js").forEach(function (file) { | ||
file = file.replace(/\.[^.]*$/, ''); | ||
var instance = require(file); | ||
@@ -66,3 +58,3 @@ var single = typeof instance == 'function'; | ||
single ? route(url, 'ALL', {ALL: instance}) : | ||
single ? route.call(router, url, 'ALL', {ALL: instance}) : | ||
methods.forEach(function (method) { | ||
@@ -72,5 +64,7 @@ if (instance[method.toLowerCase()]) { | ||
} | ||
route(url, method, instance); | ||
route.call(router, url, method, instance); | ||
}); | ||
}); | ||
return router; | ||
}; |
{ | ||
"name": "rainbow", | ||
"description": "Express router middleware for RESTful API base on certain folder path", | ||
"version": "0.5.4", | ||
"version": "1.0.0", | ||
"author": "mytharcher <mytharcher@gmail.com>", | ||
"main" : "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
}, | ||
"dependencies": { | ||
"glob": "3.x", | ||
"methods": "1.x" | ||
"glob": "7.1.2", | ||
"methods": "1.1.2" | ||
}, | ||
"peerDependencies": { | ||
"express": "^4.x" | ||
}, | ||
"devDependencies": { | ||
"axios": "0.16.2", | ||
"express": "^4.x", | ||
"mocha": "3.5.2" | ||
}, | ||
"keywords": [ | ||
@@ -27,3 +38,4 @@ "express", | ||
"node": "*" | ||
} | ||
}, | ||
"license": "MIT" | ||
} |
@@ -27,3 +27,3 @@ Rainbow | ||
// Here using Rainbow to initialize all routers | ||
rainbow.route(app); | ||
app.use('api/', rainbow()); | ||
@@ -47,5 +47,7 @@ app.listen(6060); | ||
If you need some filters, just add a `filters` array property which contains your filters in `filters/` folder to the handle function like this: | ||
If you need some filters, just add a `filters` array property which contains your filters each as a function to the handle function like this: | ||
```javascript | ||
var authorization = require('authorization'); | ||
exports.GET = function (req, res) { | ||
@@ -55,3 +57,3 @@ res.send(200, 'Simple getting.'); | ||
// add filters | ||
exports.GET.filters = ['authorization']; | ||
exports.GET.filters = [authorization]; | ||
``` | ||
@@ -86,16 +88,4 @@ | ||
You can write controllers with coffeescript using `.coffee` in example `controllers/user.coffee`: | ||
CoffeeScript file with `.coffee` suffix will not be supported from v1.0. | ||
```coffeescript | ||
exports.GET = (req, res) -> | ||
User.find(where: req.query.name) | ||
.success (user) -> | ||
res.send 200, user | ||
exports.PUT = (req, res) -> | ||
User.create(req.body) | ||
.success (user) -> | ||
res.send 201, user.id | ||
``` | ||
### Params ### | ||
@@ -128,3 +118,3 @@ | ||
Make sure the filters you need had been defined in `filters/` folder (could be changed) as same module name, because them will be required when initilizing. Here `authorization.js` is a example for intecepting by non-authenticated user before `GET` `http://yourapp:6060/something`: | ||
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`: | ||
@@ -152,6 +142,4 @@ ```javascript | ||
You could see filters is as same as a origin router in Express, just be put together in `filters/` folder to be interceptors like in Java SSH. | ||
Filters only support function from v1.0. | ||
Filters also support variable name of filter function in same file than string filter file name in filters directory (from v0.4): | ||
```javascript | ||
@@ -177,4 +165,4 @@ // controller file test.js route to [GET]/test | ||
exports.POST = function (req, res) {}; | ||
exports.POST.filters = ['validation']; | ||
exports.filters = ['session']; | ||
exports.POST.filters = [validation]; | ||
exports.filters = [session]; | ||
``` | ||
@@ -186,12 +174,11 @@ | ||
Controllers and filters default path could be changed by passing a path config object to `route` function when initializing: | ||
Controllers default path could be changed by passing a path config object to `route` function when initializing: | ||
```javascript | ||
rainbow.route(app, { | ||
controllers: '/your/controllers/path', | ||
filters: '/your/filters/path' | ||
}); | ||
app.use(rainbow({ | ||
controllers: '/your/controllers/path' | ||
})); | ||
``` | ||
These paths are all **RELATIVE** to your app path! | ||
These paths are all **ABSOLUTE** file path! | ||
@@ -198,0 +185,0 @@ ## Troubleshooting ## |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
-50%3
-25%7232
-20.67%3
50%3
Infinity%3
-70%54
-48.08%188
-6.47%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
Updated
Updated