npn install --save node-injector
app.js:
var Injector = require('node-injector')
, rootPath = __dirname
, injector = new Injector(rootPath);
injector.set('answer', 42);
injector.load('services/*.js');
injector.done(); // preparing
injector.get('service.answer') === 'answer is 42' // true
services/printAnswer.js:
module.exports = function(numberAnswer) {
var str = 'answer is ' + numberAnswer;
return str;
};
module.exports['@name'] = 'service.answer';
module.exports['@inject'] = ['answer'];
var injector = new Injector(rootPath)
injector.load('foo/bar/**/*.js')
injector.load(['foo/bar/**/*.js', 'foo/bar/**/*.json'])
injector.load(['foo/bar/**/*.js', 'foo/bar/**/*.json'], {basePath: 'app/baz'})
injector.set(key, value)
injector.setHelper(fn, ['dep1', 'dep2', 'depN'])
injector.setService(key, value, ['dep1', 'dep2', 'depN'])
injector.get(key)
injector.done()
module.exports = function(dep1, dep2) {
// ...
// return anything - object, fn, etc
}
module.exports['@name'] = 'anyNameSpace.moduleName'
module.exports['@inject'] = ['dep1', 'foo.dep2']