Dependency injection

There is no equivalent of the addInjector method available in ArangoDB 2.x controllers. Most use cases can be solved by simply using plain variables but if you need something more flexible you can also use middleware:

Old:

  1. ctrl.addInjector('magicNumber', function () {
  2. return Math.random();
  3. });
  4. ctrl.get('/', function (req, res, injected) {
  5. res.json(injected.magicNumber);
  6. });

New:

  1. function magicMiddleware(name) {
  2. return {
  3. register () {
  4. let magic;
  5. return function (req, res, next) {
  6. if (!magic) {
  7. magic = Math.random();
  8. }
  9. req[name] = magic;
  10. next();
  11. };
  12. }
  13. };
  14. }
  15. router.use(magicMiddleware('magicNumber'));
  16. router.get('/', function (req, res) {
  17. res.json(req.magicNumber);
  18. });

Or simply:

  1. const magicNumber = Math.random();
  2. router.get('/', function (req, res) {
  3. res.json(magicNumber);
  4. });