Comparing version 1.1.8 to 1.1.9
@@ -16,3 +16,3 @@ // simplistic example application for nodulejs | ||
debug = (appConfig.customDebug) | ||
? appConfig.customDebug('demoApp') | ||
? appConfig.customDebug('nodulejs->demoApp') | ||
: function(msg) { if (mergedConfig.debugToConsole) console.log('nodule demoApp: ' + msg); }; | ||
@@ -19,0 +19,0 @@ }; |
{ | ||
"name": "nodulejs", | ||
"version": "1.1.8", | ||
"version": "1.1.9", | ||
"description": "Utility for discovering and initializing node/express 'nodules'", | ||
@@ -5,0 +5,0 @@ "main": "nodule.js", |
@@ -23,3 +23,3 @@ # nodulejs | ||
1. __dirs__: <span style="color:grey">(OPTIONAL, default='/nodules')</span> *path(s) to look for your nodules, exclude property can be full or partal match* <br>__example:__ [{ path: '/app', exclude: ['demoApp.js', '.test.js'] }, { path: '/lib', exclude: ['.test.js'] }] | ||
1. __dirs__: <span style="color:grey">(OPTIONAL, default='/nodules')</span> *path(s) to look for your nodules, exclude property can be full or partal match* <br>__example:__ [{ path: '/app', exclude: ['demoApp.js', '.test.js', '/shared/'] }, { path: '/lib/nodules', exclude: ['.test.js'] }] | ||
2. __debugToConsole__: <span style="color:grey">(OPTIONAL, default=false)</span> *set to true to see nodulejs debug output in the console* | ||
@@ -29,10 +29,9 @@ 3. __customDebug__: <span style="color:grey">(OPTIONAL)</span> *custom debug function* <br>__example:__ function(identifier) { return function(msg){... your debug function here ...} } | ||
## What is a nodule? | ||
A nodule is a self-discovering, self-initializing component that would be analagous to a JSP or PHP page in those worlds. Except it has an advanatage in that its route is declared, not tied by default to the file name or file structure. So you are free to re-organize nodules without upsetting urls. But more importantly, because nodules are self-discovering, there are no onerous config files to maintain (IE - spring). This system allows a much more scalable architecture on large sites--as there are no files that grow to enormouse sizes, and files can be re-organized, placed into subfolfders, etc. with zero impact. | ||
A nodule is a self-discovering, self-initializing component that would be analagous to a JSP or PHP page in those worlds. Except it has an advanatage in that its route is declared, not tied by default to the file name or file structure. So you are free to re-organize nodules without upsetting urls. But more importantly, because nodules are self-discovering, there are no onerous config files to maintain (IE - spring). This system allows a much more scalable architecture on large sites--as there are no config or other shared files which grow to enormous sizes as the site grows, and nodules can be re-organized, placed into subfolfders, etc. with zero impact. | ||
### What does a nodule do? | ||
Not a whole lot out of the box. I split nodulejs off from it's current sole implementation, [yukon API framework](https://github.com/jackspaniel/yukon), | ||
and trimmed it down to the bare essentials. The idea/hope is that it can potentially be a building block for other frameworks. | ||
Not a whole lot out of the box. See the [yukon API framework](https://github.com/jackspaniel/yukon) for a fully-fleshed out implementation. I split nodulejs off from yukon with the idea that it can potentially be a building block for other frameworks. | ||
A nodule can have any properties you want to add, which will be propagated throughout the middleware chaing as as req.nodule. But nodulejs only cares about 4 core properties: | ||
A nodule can have any properties you want to add, which will be propagated throughout the middleware chaing as as req.nodule. But nodulejs only cares about 4 core properties, which are needed to register express middleware at app-init time: | ||
@@ -45,7 +44,5 @@ 1. __route__: <span style="color:grey">(REQUIRED)</span> *one or more express routes - can be a string, RegExp, or array of either* | ||
### Ok, what problem does this solve? | ||
__Note:__ to see the concepts described in this section actually fleshed out, see the [yukon API framework](https://github.com/jackspaniel/yukon). | ||
From a __feature-development point of view__, we wanted to give developers the flexibility of [component-based architecture](http://en.wikipedia.org/wiki/Component-based_software_engineering) as much as possible, but still keep system-wide control over the middleware chain. On a small site with a small development team the latter might not be an issue. But on a large site with devs scattered all over the globe, some kind of middleware sandbox was a necessity. | ||
Our feature devs spend 80-90% of their effort in jade templates or on the client side. For them, node componentes are often just a pass-through to the API(s), with some business logic applied to the request on the way in, and api data on the way out. Ideally they should have to learn the as little as possible of the vagaries/plumbing/whatever-your-favorite-metaphor-for-framework-stuff of node. Creating a new node component should be as easy for them as creating a new JSP - but again, without the framework losing control of the middleware chain. | ||
Our feature devs spend 80-90% of their effort in jade templates or on the client side. For them, node components are often just a pass-through to the API(s), with some business logic applied to the request on the way in, and api data on the way out. Ideally they should have to learn the as little as possible of the vagaries/plumbing/whatever-your-favorite-metaphor-for-framework-stuff of node. Creating a new node component should be as easy for them as creating a new JSP - but again, without the framework losing control of the middleware chain. | ||
@@ -71,12 +68,7 @@ From a __framework-development point of view__, we knew that as requirements evolved, we would constantly need to add default properties to each component, while hopefully causing as little disruption as possible to existing components. This is easily accomplished by adding a default property to the base config then specifying the property only in the new nodules which need it. | ||
``` | ||
// basic page example (serving multiple routes) | ||
module.exports = function(app) { | ||
return { | ||
// routes can be a string, RegExp or array of either (to match multiple routes) | ||
route: ['/', '/home', '/special'], | ||
doNoduleBusinessLogic: function(req, res) { | ||
this.debug('doNoduleBusinessLogic called'); | ||
// example of specifying a nodule property at request time | ||
this.templateName = (req.path.indexOf('special') > -1) | ||
@@ -97,7 +89,5 @@ ? 'altHomePage.jade' | ||
routeVerb: 'post', // default = get | ||
routeVerb: 'post', | ||
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! | ||
@@ -107,5 +97,2 @@ }, | ||
doPostFormBusinessLogic: function(req, res) { | ||
this.debug('doPostFormBusinessLogic called'); | ||
// process data before sending to client | ||
if (req.nodule.responseData.dbMsg.indexOf('valid data') === -1) | ||
@@ -125,8 +112,4 @@ this.customMsg = 'Form submit failed, please supply valid param1'; | ||
// set to low number to register route with express first | ||
// set to high number to register last (can be negative - like z-index) | ||
// (routes registered first take precedence) | ||
routeIndex: 1000, | ||
routeIndex: 1000, // high routes are registered last | ||
// example of using custom non-standard middleware array | ||
middlewares: [ | ||
@@ -142,24 +125,15 @@ function(req, res, next) { | ||
#### Custom config with several nodule-dependent middleware chains, and extra nodule properties | ||
(from [demoApp.js](https://github.com/jackspaniel/nodulejs/blob/master/demo/demoApp.js)) | ||
#### Demo App config | ||
(from [demoApp.js](https://github.com/jackspaniel/nodulejs/blob/master/demo/demoApp.js) - shows defining several nodule-dependent middleware chains at app init time, and adding extra nodule properties) | ||
``` | ||
var config = { | ||
dirs: [ | ||
// path(s) to look for your nodules | ||
{ path: myDir, exclude: ['demoApp.js', '.test.js'] }, // exclude can be full or partal match | ||
// multiple dirs ok | ||
{ path: myDir, exclude: ['demoApp.js', '.test.js'] }, | ||
], | ||
// set to true or or use customDebug: function(identifier) { return function(msg){... your debug function here ...} } | ||
debugToConsole: true, | ||
// config used to override nodule defaults at the app-level | ||
// NOTE: these properties will be overridden by any properties specified at the nodule-level (nodule properties->app properties->nodulejs framework default properties) | ||
noduleDefaults: { | ||
// example of using a static array of middlewares | ||
// middlewares: [doBusinessLogic, sendJsonResponse], | ||
// example of using a function to return middlewares based on nodule properties | ||
middlewares: function(nodule) { | ||
@@ -176,3 +150,3 @@ var strRoute = nodule.route.toString(); | ||
// below are examples of adding custom properties outside of nodulejs | ||
// custom properties on top of the nodulejs core properties | ||
templateName: 'default.jade', | ||
@@ -182,3 +156,2 @@ | ||
// example of adding nodule-level business logic function | ||
doNoduleBusinessLogic: function(req, res) { }, | ||
@@ -197,3 +170,2 @@ }, | ||
// call nodule-level business logic | ||
req.nodule.doNoduleBusinessLogic(req, res); | ||
@@ -207,4 +179,4 @@ | ||
#### Multiple middleware functions which make an asynchronous call to the DB (goes with Form submit example above) | ||
(from [demoApp.js](https://github.com/jackspaniel/nodulejs/blob/master/demo/demoApp.js)) | ||
#### Multiple middleware functions which make an asynchronous call to the DB | ||
(from [demoApp.js](https://github.com/jackspaniel/nodulejs/blob/master/demo/demoApp.js) - goes with Form submit example above) | ||
``` | ||
@@ -219,8 +191,5 @@ ... | ||
function doPreForm(req, res, next) { | ||
debug('doPreForm middleware executed for: ' + req.nodule.name); | ||
// call nodule-level pre-DB business logic | ||
req.nodule.doPreFormBusinessLogic(req, res); | ||
// simulating async call to DB/cache/API/etc | ||
makeDbCall({ | ||
@@ -235,2 +204,9 @@ params: req.nodule.dbParams, | ||
function doPostForm(req, res, next) { | ||
req.nodule.doPostFormBusinessLogic(req, res); | ||
next(); | ||
} | ||
// DB simulator, see /json/formSubmit.js | ||
@@ -241,11 +217,2 @@ function makeDbCall(call) { | ||
} | ||
function doPostForm(req, res, next) { | ||
debug('doPostForm middleware executed for: ' + req.nodule.name); | ||
// call nodule-level post-DB business logic | ||
req.nodule.doPostFormBusinessLogic(req, res); | ||
next(); | ||
} | ||
``` | ||
@@ -252,0 +219,0 @@ |
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
38360
209