luin / express-di

Dependency injection for Express applications
MIT License
160 stars 8 forks source link

Not work with 'composable-middleware' module #13

Open atthaboon opened 7 years ago

atthaboon commented 7 years ago

Express-di doesn't work with composable-middleware. I use it to group verify authenticate and express-di show this error when i call to create route 'Unrecognized dependency: out' express-di. out is function under 'composable-middleware' module.

// auth.isAuthenticated() ->> call composable middleware with 2 
router.post('/', auth.isAuthenticated(), controller.create);
// my authen
export function isAuthenticated() {
  return compose()
  // Validate jwt
    .use(function (req, res, next) {
      // allow access_token to be passed through query parameter as well
      if (req.query && req.query.hasOwnProperty('access_token')) {
        req.headers.authorization = 'Bearer ' + req.query.access_token;
      }
      validateJwt(req, res, next);
    })
    // Attach user to request
    .use(function (req, res, next) {
      User.findById(req.user._id).exec()
        .then(user => {
          if (!user) {
            return res.status(401).end();
          }
          req.user = user;
          next();
        })
        .catch(err => next(err));
    });
}
// error line code in express-id with dependency is 'out' value
if (!factory) {
        throw new Error('Unrecognized dependency: ' + dependency);
}
sunkuo commented 7 years ago

@atthaboon They won't be used together because they all override the function Route.verb method in express. If you want to use both of these Framework like joi-routerexpress-dicomposable-middleware, you should read the source code ,and merge them.

stalniy commented 6 years ago

yesterday I created an integration of Expressjs and Aurelia dependency injection package. Aurelia allows to define 2 separate injectors: