express-controller

Framework for controllers in express

npm install express-controller
1 downloads in the last day
2 downloads in the last week
20 downloads in the last month

build status

express-controllers

This is the step needed to transform expressjs into a complete MVC framework, by adding Controller-support. No more hassling with code in your route. express-controllers automatically set up routing based on your controllers.

Contributors

Julian Duniec http://github.com/julianduniec

Installation

$ npm install express-controller

Usage

var expressControllers = require('express-controller');

//Your express-app
var app = express();

//Tell expressControllers to use the controllers-directory, and use bind() to set up routing.
expressControllers
            .setDirectory( __dirname + '/controllers')
            .bind(app);

Controllers & Paths

The paths will be generated by a convention of naming controllers and functions.

A basic example: PeopleController.js

module.exports = {
    /*
        Will be translated to get("/people") (first level is generated by controller name)
    */
    get_index : function(req, res) {
        res.send("Hello visitor!");
    },

    /*
        Will be translated to get("/people") (HTTP-method is extracted by first item in function name)
    */
    post_index : function(req, res) {
        res.send("A post");
    },

    /*
        Will be translated to get("/people/finest") (subsections automatically appended)
    */
    get_finest : function(req, res) {
        res.send("A post");
    },

    /*
        Will be translated to get("/people/:id") (parameters automatically extracted from function parameters)
    */
    get_index : function(req, res, id) {
        res.send("You are requesting the resource with id: " + id);
    },

    /*
        Will be translated to get("/people/:id/friends") (if parameter is included in function-name, it will be be included in the same position)
    */
    get_id_friends : function(req, res, id) {
        res.send("You are requesting the friends of the person with id: " + id); 
    }
    /*
        Will be translated to get("/people/:userName/friend-requests") (non parameter parts that use camelCase will be separated by hyphens in the url)
    */
    get_userName_friendRequests : function(req, res, userName) {
        res.send("You are requesting the friend requests of the person with user name: " + userName);
    }
}
npm loves you