Comparing version 1.1.4 to 1.1.5
@@ -1,3 +0,4 @@ | ||
// example of using routeIndex to catch all routes not specifically handled | ||
// example of using high routeIndex to register route last and catch all routes not specifically handled | ||
// example of using custom non-standard middleware array | ||
module.exports = function(app) { | ||
@@ -9,12 +10,13 @@ return { | ||
// set to low number to register route with express first | ||
// high number to register last (can be negative - like z-index) | ||
// routes registered first take precedence | ||
// set to high number to register last (can be negative - like z-index) | ||
// (routes registered first take precedence) | ||
routeIndex: 1000, | ||
// example of using custom non-standard middleware array | ||
// can also be a function which returns an array of middleware functions (where *this* = this nodule) | ||
// NOTE: this function is called at app boot time, not request time | ||
// can also be a function(nodule) which returns an array of middleware functions (see demoApp.js for example) | ||
// NOTE: if using a function, it is executed at app init time, not request time | ||
middlewares: [ | ||
function(req, res, next) { | ||
res.send('404 error!'); | ||
req.nodule.debug('404 error middleware called!'); | ||
res.send('<html><body><h1>404 error!</h1></body></html>'); | ||
} | ||
@@ -21,0 +23,0 @@ ] |
// simplistic example application for nodulejs | ||
var nodulejs = require('nodulejs'); | ||
var path = require('path'); | ||
var _ = require('lodash'); | ||
var nodulejs = require('../nodule'); | ||
var debug; | ||
module.exports = function(app) { | ||
// nodulejs finds and loads nodules based on config below | ||
// then registers routes with express based on nodule route, routeIndex and routeVerb | ||
nodulejs(app, config); | ||
module.exports = function(app, appConfig) { | ||
// 1) finds and loads nodules based on config below | ||
// 2) registers routes with express based on nodule properties: route, routeIndex, routeVerb and middlewares | ||
var mergedConfig = _.merge(config, appConfig || {}); | ||
nodulejs(app, mergedConfig); | ||
debug = function(msg) { | ||
if (mergedConfig.debugToConsole) console.log('nodule demoApp: ' + msg); | ||
}; | ||
}; | ||
// since we're not sure where this demo app is being invoked | ||
var myDir = __filename.substr(0,__filename.length-11); | ||
// override nodulejs defaults | ||
@@ -15,7 +26,9 @@ var config = { | ||
dirs: [ | ||
{ path: '/demo', exclude: ['demoApp.js', '.unit.js'] }, // exclude can be full or partal match | ||
// path example | ||
{ path: myDir, exclude: ['demoApp.js', '.test.js'] }, // exclude can be full or partal match | ||
// multiple dirs ok | ||
], | ||
// also can use customDebugger property with functin of format: function(identifier) { return function(msg){...} } | ||
// or use customDebugger property with function of format: function(identifier) { return function(msg){...} } | ||
debugToConsole: true, | ||
@@ -31,3 +44,7 @@ | ||
middlewares: function(nodule) { | ||
if (nodule.route.indexOf('/json/') > -1) | ||
var strRoute = nodule.route.toString(); | ||
if (nodule.routeVerb === 'post') | ||
return [doPreForm, doPostForm, sendJsonResponse]; | ||
else if (strRoute.indexOf('/json') === 0) | ||
return [doBusinessLogic, sendJsonResponse]; | ||
@@ -38,10 +55,26 @@ else | ||
// example of adding custom property | ||
templateName: null, | ||
// below are examples of adding custom properties outside of nodulejs | ||
templateName: 'default.jade', | ||
templateDir: null, | ||
// example of adding nodule-level business logic | ||
doNoduleBusinessLogic: function(req, res) { }, | ||
}, | ||
}; | ||
//////////////////////////////////////////////////////////////////// | ||
/// middlwares examples for simple HTML and JSON request/response // | ||
//////////////////////////////////////////////////////////////////// | ||
function doBusinessLogic(req, res, next) { | ||
console.log('middleware doBusinessLogic executed for: ' + req.nodule.name); | ||
debug('doBusinessLogic middleware executed for: ' + req.nodule.name); | ||
// app-level business logic can go here | ||
// call nodule-level business logic | ||
req.nodule.doNoduleBusinessLogic(req, res); | ||
// app-level business logic can also go here | ||
next(); | ||
@@ -51,9 +84,58 @@ } | ||
function sendJsonResponse(req, res, next) { | ||
console.log('middleware sendJsonResponse executed for:' + req.nodule.name); | ||
res.send({msg:'middleware finished for nodule: ' + req.nodule.name}); | ||
debug('sendJsonResponse middleware executed for: ' + req.nodule.name); | ||
// app-level presentation logic, or stuff like reporting can go here | ||
res.send({ | ||
msg: req.nodule.customMsg || 'all middleware finished for nodule: ' + req.nodule.name, | ||
id: req.nodule.customId || req.params.id, | ||
data: req.nodule.responseData | ||
}); | ||
} | ||
function sendHtmlResponse(req, res, next) { | ||
console.log('middleware sendHtmlResponse executed for:' + req.nodule.name); | ||
res.render(req.nodule.templateName, {msg:'middleware finished for nodule: ' + req.nodule.name}); | ||
debug('sendHtmlResponse middleware executed for: ' + req.nodule.name + ', template:' + req.nodule.templateName); | ||
// app-level presentation logic, or stuff like reporting can go here | ||
req.nodule.templatePath = path.join((req.nodule.templateDir || req.nodule.path), req.nodule.templateName); | ||
res.render(req.nodule.templatePath, {msg:'all middleware finished for nodule: ' + req.nodule.name}); | ||
} | ||
/////////////////////////////////////////////////////////////////////////////////////// | ||
/// middleware examples for typical form request/response with simualted going to DB // | ||
/// | ||
/// see ./json/submitForm.js for implementation // | ||
/////////////////////////////////////////////////////////////////////////////////////// | ||
function doPreForm(req, res, next) { | ||
debug('doPreForm middleware executed for: ' + req.nodule.name); | ||
// call nodule-level pre-form business logic | ||
req.nodule.doPreFormBusinessLogic(req, res); | ||
// simulating async call to DB/cache/API/etc | ||
makeDbCall({ | ||
params: req.nodule.dbParams, | ||
callback: function(err, response) { | ||
req.nodule.responseData = response; | ||
next(); | ||
} | ||
}); | ||
} | ||
// DB simulator, see /json/formSubmit.js | ||
function makeDbCall(call) { | ||
var response = (call.params.param1) ? 'valid data, param1='+call.params.param1 : 'missing param1, please resubmit'; | ||
call.callback(null, {dbMsg:response}); | ||
} | ||
function doPostForm(req, res, next) { | ||
debug('doPostForm middleware executed for: ' + req.nodule.name); | ||
// call nodule-level post-form business logic | ||
req.nodule.doPostFormBusinessLogic(req, res); | ||
next(); | ||
} | ||
// basic JSON request example | ||
module.exports = function(app) { | ||
return { | ||
// routes can be a string, RegExp or array of either (to match multiple routes) | ||
route : '/json/getData/:id', | ||
route : '/json/getData/:id', | ||
}; | ||
}; |
@@ -1,2 +0,3 @@ | ||
// basic post form submit example | ||
// basic post form-submit example | ||
module.exports = function(app) { | ||
@@ -8,3 +9,17 @@ return { | ||
routeVerb: 'post', // default = get | ||
doPreFormBusinessLogic: function(req, res) { | ||
this.debug('doPreFormBusinessLogic called'); | ||
// process form parameters etc | ||
this.dbParams = {param1: req.body ? req.body.param1 : null}; // in real life don't forget to sanitize query params! | ||
}, | ||
doPostFormBusinessLogic: function(req, res) { | ||
this.debug('doPostFormBusinessLogic called'); | ||
// process data before sending to client | ||
if (req.nodule.responseData.dbMsg.indexOf('valid data') === -1) | ||
this.customMsg = 'Form submit failed, please supply valid param1'; | ||
} | ||
}; | ||
}; |
@@ -54,2 +54,3 @@ var glob = require('glob'); | ||
var debug = defaultConfig.customDebug('nodulejs'); | ||
debug('debug initialized'); | ||
@@ -67,2 +68,3 @@ // find all nodules and init all routes first so they can be sorted based on routeIndex | ||
function loadNodules(dir, exclude) { | ||
debug('loadNodules called - dir: ' + dir + ', exclude: ' + exclude); | ||
var root = dir || process.cwd(); // TOOD - should this be process.cwd() + '/app' ? | ||
@@ -80,2 +82,3 @@ glob.sync('./**/*.js', { cwd: root }) | ||
seedNodule.name = path.basename(filepath, '.js'); | ||
seedNodule.debug = defaultConfig.customDebug(seedNodule.name); | ||
@@ -86,4 +89,3 @@ // nodules can have multiple routes | ||
seedNodules[routePath] = seedNodule; // routes must me unique | ||
// middlewares can be an array of functions, or function that returns an array of functions | ||
@@ -102,6 +104,3 @@ var middlewares = typeof seedNodule.middlewares === 'function' ? seedNodule.middlewares(seedNodule) : seedNodule.middlewares; | ||
_.each(routes[key], function(route) { | ||
debug('registering route: ' + route.verb + ' ' + route.path); | ||
if (defaultConfig.debugToConsole) console.log('registering route: ' + route.verb + ' ' + route.path); | ||
app[route.verb].apply(app, [route.path, initRequest].concat(route.middlewares)); | ||
@@ -108,0 +107,0 @@ }); |
{ | ||
"name": "nodulejs", | ||
"version": "1.1.4", | ||
"version": "1.1.5", | ||
"description": "Utility for discovering and initializing node/express 'nodules'", | ||
@@ -24,5 +24,8 @@ "main": "nodule.js", | ||
"devDependencies": { | ||
"body-parser": "^1.10.1", | ||
"express": "^4.10.6", | ||
"jade": "^1.8.2", | ||
"mocha": "^2.1.0", | ||
"sinon": "^1.12.2" | ||
"supertest": "^0.15.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
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
29417
15
361
5