eggnog
What require() should be.
See the wiki for complete documentation.
eggnog is a simple, lightweight dependency injection framework for NodeJs
- Designed for making modular applications easy to write
- Minimal boilerplate -- Convention over configuration
- No config files or factories to maintain -- eggnog crawls your project for you
- Dependency injection allow for easier testing
- No need to require any special dependencies in your files -- eggnog acts more like a spec than a library
Link to NPM
Current Version: 1.3.0
Let's assume this is file structure for our application:
index.js
package.json
node_modules/
express/
...
src/
server/
index.js
utils/
config.js
Here's what our src/server/index.js
module might look like:
module.exports = function(
serverPort,
express,
console
os) {
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
var server = app.listen(serverPort, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s on %s', host, port, os.type());
});
return app;
};
Our src/utils/config.js
looks like this:
module.exports = function() {
return {
serverPort: 8080,
foo: true,
barUser: 'Mikey',
...
};
};
Finally, index.js
pulls everything together
var eggnog = require('eggnog');
var context = new eggnog.Context('./src');
var app = context.loadModule('server/index');
Launching our application is nothing special:
node index.js
That's it! eggnog will handle the rest.
What about unit testing?
var eggnog = require('eggnog');
var sinon = require('sinon');
var context = new eggnog.TestContext('/src');
var express = sinon.spy();
...
var app = context.createModule('server/index', {
'utils/config.serverPort': 8080,
'lib::express': express,
'global::console': { log: function() {} }
});
...