Inject dependencies into koa apps easily.
'use strict';
var koa = require('koa');
var Router = require('koa-router');
var inject = require('koa-inject');
var router = new Router();
var app = koa();
var injector = inject.init(app, {
'db': {
a: 6
},
'db2': {
b: 7
}
});
injector.set('User', {
name: 'User'
});
app.use(router.middleware());
router.get('/', inject(function * (db, db2, User) {
this.body = [
User,
db
];
}));
app.listen(3031);
Injects dependencies into generator function from koa application context. Returns a new generator function.
var Router = require('koa-router');
var inject = require('koa-inject');
var router = new Router();
module.exports = router.middleware();
router.get('/', inject(function * (deps...) {
}));
Initializes dependency injection into koa app. Returns an instance of simple-injector Injector
.
var koa = require('koa');
var inject = require('koa-inject');
var app = koa();
var injector = inject.init(app);
$ npm test