Express-di

Bring dependency injection to the Express

View the Project on GitHub luin/express-di

express-di

Build Status

Installation

npm install --save express-di

Usage

To get started simply require('express-di') before var app = express(), and this module will monkey-patch Express, allowing you to define "denpendencies" by providing the app.factory() method, after which you can use the "denpendencies" in you routes following the Dependency Injection pattern(DI).

Example

In the past, if you want to pass variables between middlewares, you have to tack on properties to req, which seems odd and uncontrollable(that you couldn't point out easily which middleware add what properties to req). For example:

var express = require('express');
var app = express();

var middleware1 = function(req, res, next) {
  req.people1 = { name: "Bob" };
  next();
};

var middleware2 = function(req, res, next) {
  req.people2 = { name: "Jeff" };
  next();
};

app.get('/', middleware1, middleware2, function(req, res) {
  res.json({
    people1: req.people1,
    people2: req.people2
  });
});

require('http').createServer(app).listen(3008);

After using express-di, you can do this:

var express = require('express');
// Require express-di
require('express-di');
var app = express();

app.factory('people1', function(req, res, next) {
  next(null, { name: "Bob" });
});

app.factory('people2', function(req, res, next) {
  next(null, { name: "Jeff" });
});

app.get('/', function(people1, people2, res) {
  res.json({
    people1: people1,
    people2: people2
  });
});

require('http').createServer(app).listen(3008);

Define a dependency

The app.factory(name, fn) method is used to define a dependency.

Arguments

Default dependencies

express-di has defined three default dependencies: req, res and next, so that you can use these arguments in your router middlewares just as before.

Cache

The same dependency will be cached per request. For instance:

app.factory('me', function(req, res, next) {
  // This code block will only be executed once per request.
  User.find(req.params.userId, next);
});

var checkPermission = function(me, next) {
  if (!me) {
    return next(new Error('No permission.'));
  }
  next();
};

app.get('/me', checkPermission, function(me, res) {
  res.json(me);
});

Where can I use DI?

You can use DI in your route-specific middlewares(aka app.get(), app.post(), app.put()...).

Performance

The process of DI will only be executed once at startup, so you don't need to worry about the performance.

You can test the performance using make bench.

Test

Articles and Recipes

License

The MIT License (MIT)

Copyright (c) 2014 Zihua Li

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Bitdeli Badge