di-ioc

Dependency injection for Node.js apps

npm install --save di-ioc
Read the docs View on GitHub

Example project structure:

  • bin
    • www
  • lib
    • app
      • server.js
    • models
      • user.js
    • inf
      • db.js
    • index.js
lib/index.js
module.exports = require('di-ioc')
.create()
.use('./inf/*')
.use('./models/*')
.use('./app/*');
            
lib/app/server.js
module.exports = User => {
  return express()
  .get('/users', (req, res, next) => {
    User.find({}, (err, users) => {
      if (err) return next(err);
      res.json(users);
    });
  }));
}
            
lib/models/user.js
module.exports = db => {
  return db.model('User', {
    name: String,
    email: String
  }
};
            
bin/www.js
var Example = require('./lib');
var example = Example.create();
example.app.server.listen(8080);

            
lib/inf/db.js
module.exports = () => {
  var mongoose = require('mongoose');
  mongoose.connect('mongodb://localhost/test');
  return mongoose;
}
            

Copyright © 2015 Anthony Foster.

NPM Version Build Status Coverage Status NPM Downloads License