A lightweight IoC container to build the core of a scalable and modular API (inspired by Angular and Pimple). No dependencies, works on Node.js and in the browser (only 2kb minified gzipped).
npm install noder.io

Noder.io provides a lightweight and flexible core to create a scalable API of a lib, a module, an application or a framework. Noder.io is inspired (among others) by _Angular_ and _Pimple_.
It is useful for __starting a project quickly__ with a __modular API__ ready to use.
Noder.io (and any object built on top of Noder.io) integrates:
* dependency injection
* constructors of services, factories and providers
* lazy loading
* plugins system easy to use
No dependencies, works on __Node.js__ and in the __browser__ (only 7kb minified - 2kb gzipped).
See quickstart.
Get common instance of Noder:
``js`
var noder = require('noder.io');
Best practice, create an instance of Noder class for your project:
`js
// ./api/index.js
var Noder = require('noder.io').Noder;
var api = new Noder();
// code body that constructs your API
module.exports = api;
`
or shortcut:
`js`
// ./api/index.js
module.exports = require('noder.io').createNoder();
Use your API in another file:
`js
var api = require('./api');
// load a plugin
api.use('pluginName');
// create an item in the container
api.$di.set('someItem', 'value of the item');
// ...
`
Noder.io provides a class to handle a collection of items.
`js
// create a collection
var items = noder.createCollection();
items.set('keyName', 'key value');
// keyName value
console.log(items.get('keyName'));
// get all items
var all = items.getAll();
// true
console.log(items instanceof noder.Collection);
`
See collection.
See dependency injection.
noder.$require method provides a lazy require():
`js
// define the property without loading the mongoose module
noder.$require('mongoose');
// false
console.log(noder.$require.isLoaded('mongoose'));
// lazy loading
var mongoose = noder.mongoose;
// true
console.log(noder.$require.isLoaded('mongoose'));
// true
console.log(noder.mongoose === require('mongoose'));
`
Aliases:
`js
noder.$require('promise', 'bluebird');
// true
console.log(noder.promise === require('bluebird'));
`
Custom loader:
`js
// factory: promisify the "fs" module
noder.$require('fs', function() {
return noder.promise.promisifyAll(require('fs'));
});
fs.readFileAsync('./any-file.js')
.then(function(contents) {
console.log(contents);
})
.catch(function(err) {
console.error(err);
})
;
`
See lazy loading.
Noder.io provides a plugin system to make a package works as a plugin for Noder.io and also as a standalone module or library.
Example of a Noder plugin:
`jsNoder
/**
* Initialization for use as a standalone module.
* @return {Noder} New instance
*/
module.exports = function blog() {
var Noder = require('noder.io').Noder;
var noder = new Noder();
// or use the shortcut:
// var noder = require('noder.io').createNoder();
return module.exports.__noder(noder);
};
/**
* Init blog plugin.Noder
* @param {Noder} noder instanceNoder
* @return {Noder} Current instance
*/
module.exports.__noder = function blogPlugin(noder) {
// create config object only if not exists
noder.$di.addOnce('config', {}, true);
// sub-modules of blogPlugin
// that add features to the instance of Noder
noder.use(require('./api/article'));
noder.use(require('./api/comment'));
noder.use(require('./api/admin'));
// Always return the instance of Noder to allow chaining
return noder;
};
``
See plugins.
Noder.io is fully tested with Unit.js and Mocha.
MIT (c) 2013, Nicolas Tallefourtane.
|  |
|---|
| Nicolas Talle |
|  |