Flexible controllers for express.js apps
Flexible controllers for express.js apps.
Easily load your controllers, no matter where they are, and mount their actions/routes to your app.
npm install exctrl
var express = require'express'app = expressexctrl = require'exctrl';// configuring express app...exctrlloadapp pattern: __dirname + '/controllers/*.js';
// ...ressend'user created';;// ...ressend'user read';;// ...ressend'user updated';;// ...ressend'user deleted';;// ...ressend'all users';;
POST /user GET /user/:id PUT /user/:id DELETE /user/:id GET /user
An URL generated by
exctrl has the format:
[/<prefix>]/<controller name>[/<method name>][/<...>]
An optional prefix to add to all mounted routes, and is provided via the
options hash, see the
load function below.
The main mount point for each controller and can be provided in three different ways:
optionshash for options on how to extract the name)
nameproperty of the controller if it's a string
mountfunction, see below
Some method names are considered "magic", in that way that it may imply both HTTP method and URL:
<method name>is left empty), e.g.
If a method name starts with a known HTTP method, i.e.
delete, it is mounted as that method in the provided express.js app, otherwise it's considered a
The HTTP method part from the method name is then cut off, and the remainings are dasherized (e.g.
"getBestFriends" -> "best-friends") and added to the url.
A controller action mustn't be just a function, e.g:
var basicController =name: 'basic'// ROUTE: GET /basic/stuff;
For more advanced usage you can declare a controller action as an array (like AngularJS dependency injection syntax), e.g:
var /* ... */ ;var advController =name: 'adv'getStuff: ':type' middleware ':anotherparam' 'chunk'// ROUTE: GET /adv/stuff/:type/:anotherparam/chunkif reqparamstype === 'other'thisotherStuffreq res; // Yes, `this` here is the actual controllerelse/* do something else */// ROUTE: GET /adv/other-stuffressend/* other stuff */;
app- Your express.js app
options- A hash of options
"**/*.ctrl.js"a nameRegExp as
/([^\/\\]+).ctrl.js$/can be used to extract
/var/tmp/dir/user.ctrl.js. The first match group will be used as extractor, and full path filename will be used when matching.
api, controller name
getthe mounted route will be:
load() loads your controllers, and does so syncronously to make sure the routes are loaded at the time you want, to not interfere with other express.js configurations.
app- Your express.js app
bind() binds your app to
exctrl, so that you can skip first param to the
load function above.
name- The controller name, used as mount point
controller- A controller with methods as actions
mount() is mainly used internally by
exctrl for mounting a controller at a mount point, but is available publicly for convenience, and will stay in the API.
Note if the controller object has a
name property with type
String the first parameter to this function can be omitted, and the
controller.name will be used as mount point instead.
var name = 'user'controller =ressend/* all users */;;exctrlbindappmountname controller;// orcontrollername = name;exctrlbindappmountcontroller;
exctrl uses Semantic Versioning as versioning model.
Please feel free to contribute and send pull requests!