xcontroller

Lightweight controller framework for express

npm install xcontroller
4 downloads in the last week
16 downloads in the last month

xcontroller

NodeJS controller framework for express. Makes your controller more easily testable.

Usage

Configuration:

var app = require('express')()
var controllers = require('express-controller');

controllers({
  dir: './controllersPath', /* the path where the controllers are located */
  express: app, /* optionally pass in express to automatically register the routes */
  verbose: true /* writes debug logging to the console */
});

Example Controller:

module.exports = {

  options: {
    baseRoute: '/myRoute'
  },

  /*
      method: defaults to 'get'
      route: uses the base route of '/myRoute'
  */
  index: {
    handler: function (req, res) {
      res.send("Hello World!");
    }
  },

  /*
      handles a post reqest
      add some middleware
      sets a route of '/myroute/:id/edit'
  */
  edit: {
    route: '/:id/edit',
    method: 'post',
    middleware: requireLogin /* some middleware */,
    handler: function (req, res) {
      res.send "Editing something"
    }
  }

};

If you're using express you can pass in the instance in the options which will automatically register each route. If you're not using express you can manually register each route.

var controllers = require('express-controller');
controllers("./controllers", function(routes){ 
  /*
    Route:

      name
      route
      middleware: [] or function(req, res, next)
      method: 'get', 'post', 'put', 'delete'
      handler: function(req, res)

    Manually register with express

      app[method](route, middleware, handler)
  */
});
npm loves you