Comparing version 1.0.11 to 1.1.0
@@ -5,29 +5,41 @@ var glob = require('glob'); | ||
module.exports = function(app, appConfig, customDebug) { | ||
module.exports = function(app, config) { | ||
var debug = customDebug || function(){}; | ||
var seedNodules = [], routes = {}; | ||
var config = { | ||
// NOTE: the three route config params below must be specified in the nodule init method, they cannot be mutated at request-time | ||
// REQUIRED, must be unique within express app, can be String or RegExp or an array of either to handle multiple routes | ||
route: null, | ||
var rootConfig = { | ||
// set this to true if you have not defined a customDebugger but want to temporality see debugging output | ||
debugToConsole: false, | ||
// get/post/put/delete | ||
routeVerb: 'get', | ||
// directories to look for nodules in, minus exclude pattern | ||
dirs: [ | ||
{ path: process.cwd(), exclude: null } | ||
], | ||
// use to load routes before or after main group (use negative #s for to load route first - like z-index) | ||
routeIndex: 0, | ||
debugToConsole: false | ||
noduleDefaults: { | ||
// the array of middleware functions which will be called in order for each nodule | ||
middlewares: [], | ||
// NOTE: the three route config params below must be specified in the nodule init method, they cannot be mutated at request-time | ||
// REQUIRED, must be unique within express app, can be String or RegExp or an array of either to handle multiple routes | ||
route: null, | ||
// get/post/put/delete | ||
routeVerb: 'get', | ||
// use to load routes before or after main group (use negative #s for to load route first - like z-index) | ||
routeIndex: 0, | ||
}, | ||
}; | ||
var defaultConfig = _.extend(_.cloneDeep(config), appConfig); | ||
var defaultConfig = _.merge(_.cloneDeep(rootConfig), config); | ||
var debug = config.customDebug || function(msg) { if (defaultConfig.debugToConsole) console.log(msg); }; | ||
return { | ||
loadNodules: loadNodules, | ||
registerRoutes: registerRoutes | ||
}; | ||
// find all nodules and init all routes first so they can be sorted based on routeIndex | ||
defaultConfig.dirs.forEach(function(dir) { loadNodules(dir.path, dir.exclude); }); | ||
registerRoutes(); | ||
// finds nodules in supplied dir, minus exclude patterns, and invokes initNodule method on them | ||
function loadNodules(dir, exclude) { | ||
@@ -40,5 +52,6 @@ var root = dir || process.cwd(); // TOOD - should this be process.cwd() + '/app' ? | ||
// creates seedNodules for each found nodule (seedNodules are cloned at the beginning of each request and added to the req object) | ||
function initNodule(filepath) { | ||
var config = require(filepath)(app); | ||
var seedNodule = _.extend(_.cloneDeep(defaultConfig), config); // merge config properties onto default config | ||
var seedNodule = _.merge(_.cloneDeep(defaultConfig.noduleDefaults), config); // merge config properties onto default config | ||
seedNodule.path = path.dirname(filepath); | ||
@@ -54,8 +67,8 @@ seedNodule.name = path.basename(filepath, '.js'); | ||
routes[seedNodule.routeIndex] = []; | ||
routes[seedNodule.routeIndex].push({path:routePath, verb:seedNodule.routeVerb}); | ||
routes[seedNodule.routeIndex].push({path:routePath, verb:seedNodule.routeVerb, middlewares:seedNodule.middlewares}); | ||
}); | ||
} | ||
// register routes in order based on nodule.routeIndex (default 0, can be negative) | ||
function registerRoutes(middlewares) { | ||
// register express routes (call app.get()/app.post() etc.) in order based on nodule.routeIndex (default = 0, can be negative) | ||
function registerRoutes() { | ||
var sortedRouteKeys = _.sortBy(_.keys(routes), function(num){ return 1*num; }); | ||
@@ -68,3 +81,3 @@ _.each(sortedRouteKeys, function(key) { | ||
app[route.verb].apply(app, [route.path, initRequest].concat(middlewares)); | ||
app[route.verb].apply(app, [route.path, initRequest].concat(route.middlewares)); | ||
}); | ||
@@ -74,3 +87,3 @@ }); | ||
// attach new nodule instance to each incoming request | ||
// first step in middleware chain - clone applicable seedNodule and attach cloned instance to each incoming request | ||
function initRequest(req, res, next) { | ||
@@ -77,0 +90,0 @@ req.module = _.cloneDeep(seedNodules[req.route.path]); |
{ | ||
"name": "nodulejs", | ||
"version": "1.0.11", | ||
"version": "1.1.0", | ||
"description": "Utility for discovering and initializing node/express 'nodules'", | ||
@@ -24,4 +24,5 @@ "main": "nodule.js", | ||
"devDependencies": { | ||
"mocha": "^2.1.0" | ||
"mocha": "^2.1.0", | ||
"sinon": "^1.12.2" | ||
} | ||
} |
var assert = require('assert'); | ||
var sinon = require('sinon'); | ||
var nodulejs = require('../nodule.js'); | ||
@@ -7,9 +8,16 @@ | ||
describe('app', function(){ | ||
var nodule = nodulejs(app, appConfig); | ||
describe('app', function(){ | ||
it('should initialize', function(done){ | ||
var nodule = nodulejs(app, appConfig); | ||
assert(typeof nodule === 'object'); | ||
done(); | ||
}) | ||
}); | ||
it('should find .js files but exclude .nu.js files', function(done){ | ||
var spy = sinon.spy(); | ||
var proxy = once(spy); | ||
// nodule.loadModules(null, 'nu.js'); | ||
done(); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17025
7
93
2
4