Node.js sample application using dependency injection

I really like how dependency injection is done in angular, so I would like to use a similar pattern in node. There are many modules that are typically DI containers and are not easy to select.

Are there any examples of using DI in node in the world? How to structure the application in this case?

+4
source share
2 answers

I had a very sexy answer to the mockery of the tests, then I re-read your question and ... ugh.

The whole mechanism exports/ requireprovides dependency injection, and you can make fun or replace any module you need by simply changing require('module')to point to a new one.

You can even wrap any alternative API compatible libraries that you might want to disable. For example, if you were approaching a nervous breakdown, deciding to switch from underscore to lodash , you can simply create a module called underdash.js as follows:

if (process.env.COLD_SWEATS || process.env.SHAKING || process.env.PALPITATIONS) {
    module.exports = exports = require('underscore');
} else {
    module.exports = exports = require('lodash');
}

And then, when you need it, it’s simple require('./underdash'), and now you don’t need to worry about which library you are using down and can change it when you switch the switch - LONG LIKE THE API IS ALSO USED, otherwise you will have a bad time.

+1

systemic . electrician, TES ( node.js). , angular DI. diogenes.

.

0

All Articles