node package manager
It’s your turn. Help us improve JavaScript. Take the 2017 JavaScript Ecosystem Survey »

composey

composey npm version MIT license

Simple route module loading for Express with dependency injection

Install

npm install composey --save

Usage

Compose the route modules

var express = require('express');
var compose = require('composey');
 
var app = express();
 
compose({
  app: app,
  routes: {
    './lib/some-module': {
      imports: {
        anotherModule: require('another-module'),
        myModule: require('my-module')
      }
    }
  }
});
 
app.listen(3000, function () {
  console.log('server started');
});

Example module ./lib/some-module

module.exports = function (imports) {
  var router = imports.Router();
  var anotherModule = imports.anotherModule;
  var myModule = imports.myModule;
  
  router.get('/testing', function (req, res) {
   res.send('done');
  });
  
  return router;
};

API

compose(options

  • options
    • app - Express app object. If you don't provide an app object, it creates one for you.
    • imports - Default imports to pass to all routes
    • root - the root directory to prepend to the path of the modules. Defaults to relative to calling file __dirname
    • routes - a key/value map of route modules to load. The key is the path to the route module, and the value is another key/value map stating which modules to inject.
    • mount - the path the mount the route at. Defaults to /

Run Tests

npm install
npm test