simple-controllers

Add to Express a simple controllers management.

npm install simple-controllers
4 downloads in the last week
16 downloads in the last month

simple-controllers

Add to Express a simple controllers management.

Installation

Via npm:

$ npm install simple-controllers

Usage

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);

Controllers

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: {...} };
}

Actions

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) { ... }

Authentication

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']);

License

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.
npm loves you