1.0.1 • Published 5 years ago

controller-base v1.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

controller-base

A controller template for MVC in Express.

Getting Started

Installing

$ npm install controller-base

Usage

The controller follows the Ruby on Rails methodology.

The controller will register the following action methods by default:

  • $index
  • $new
  • $create
  • $show
  • $edit
  • $update
  • $destroy

Each method has the following signature: $action(request, response, next).

Simply extend the controller class and override the $action methods and add your own actions as needed.

// controllers/account.js
const Controller = require('controller-base');

class AccountsController extends Controller {
    $index(request, response, next) {
        // List all accounts...
    }
    
    // Implement the other $action methods as needed.
    
    // Add a custom $action
    $action(request, response, next) {
        response.send('/action');
    }
    
    // Override the configure method to register your custom action.
    configure(router) {
        // Call the super method to configure the default actions.
        super.configure(router);
        
        // And configure your custom action.
        const self = this;
        router.get('/action', (request, response, next) => self.$action(request, response, next));
    }
}

module.exports = new AccountsController();

Then call the configure method on your controller instance and pass it a Router object.

const AccountsController = require('./controllers/account');

const router = new express.Router();
AccountsController.configure(router);

// ...
app.use('/accounts', router);

Contributing

This project uses the git branching model described here.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License. See the LICENSE.md file for details.

Acknowledgments