simple-controllers

Add to Express a simple controllers management.

simple-controllers

Add to Express a simple controllers management.

Via npm:

$ npm install simple-controllers

To start, here it shows a basic example:

// filename: app.js
...
var controllers = require('simple-controllers');
...
app.configure(function() {
    ...
    // Specify the path of the controllers
    app.set('controllers', __dirname + '/controllers');
    ...
});
...
var result = app.controllers();
console.dir(result);

By default all Controllers response in JSON, so to execute a return with the objects.

Example with basic structure of a function:

// filename: session.js

// public function
exports.is_auth = function(req, res) {
    ...
    return { is_auth: true, user: {...} };
}

By default all Actions using method GET. Add the prefix get_ or post_ to indicate the method used.

Example action with method GET:

// public function
exports.get_is_auth = function(req, res) { ... }

Or

// public function
exports.is_auth = function(req, res) { ... }

Example action with method POST:

// public function
exports.post_add_user = function(req, res) { ... }

Example with authentication function to filter access:

...
var auth_fn = function(req, res, next) {
    // Here your authentication code
    ...
    if (is_auth()) {
        next();
        return;
    }
    
    // Error 401 - Unauthorized
    res.send('Authentication required', 401);
}
...
var result = app.controllers(auth_fn);
console.dir(result);

Example with authentication function and controllers without authentication:

...
var result = app.controllers(auth_func, ['session']);
The MIT License (MIT)

Copyright (c) 2012 Gerard Muñoz <gmunoz1979@gmail.com>

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.