a structural aid for creating express routes.
This code sets up an app with 3 handlers, 4 routes, and some middleware which
applies to different handler groups.
### Usage
Create a new controller by requiring controller and calling it as a function,
like this:
var controller = require('controller');
var users = controller();
Then attach it to an instance of express, as if it were middleware:
var app = require('express')();
app.use(users);
app.use('/users/', users);
### Middleware Groups
Controller introduces the idea of middleware groups. This allows to you specify
named groups, and apply to middleware to every handler that is labelled with
this group. For example, you might have bunch of handlers that require you to
be logged in, and some middleware which checks authentication. You could add
all of the handlers to the 'require-login' group, and then add your auth
middleware to the 'reguire-login' group. This will now apply that middleware to
every handler in the group.
In action:
var stuff = controller();
stuff.define('sensitive-thing', ['require-login'], function(req,res){});
stuff.define('secret-thing', ['require-login'], function(req,res){});
stuff.middleware('require-login', function(req, res, next) {
if (isAuthenticated(req)) {
next();
} else {
res.send(403);
}
})
Special Groups
There are some special groups by default. The first one is all
, which applies
middleware to every action on this controller. Apart from that, every action
name is also a middleware group, so you can add middleware to individual actions
Middleware Ordering
'all'
grouped middleware is executed first.- all other groups are then executed in the order they were added to the route
with
route
or direct
. within the group, middlewares are executed in the
order they were added. - route specific middleware is then executed in the order it was added.
### define(name, [groups], handler)
Define a handler. A handler is a function that is called as the result of a
route being visited. This does not route the handler, it only creates it, ready
for routing.
Parameters
name
- the name of the handlergroups
(optional) - the groups to add this handler to, for the purpose of
applying middleware to groups of handlers.handler
- the function that is called when the route is visited.
Example
users.define('view', function(req, res) {
res.send(Users.read(req.params.id));
});
users.define('edit', ['require-login'], function(req, res) {
Users.update(req.params.id, req.body);
res.send(200);
});
### middleware(group\*, middleware\*)
Define some middleware(s) for a group(s). More than one middleware can be
passed, as well as more than one group. If you were to pass two groups and two
middlewares, each middleware would be added to both groups.
group
has some special values. 'all'
indicates that the middleware should
apply to every route on this controller. If you pass the name of an action as
the group, the middleware will apply to that action only.
Paramaters
group
- defaults to 'all'
middleware
- middleware to add to group
.
Example
users.middleware('auth', function checkAuthd(req, res, next) {
});
users.middleware(function(res, req, next) {});
users.middleware('getUser', function(req, res, next) {});
### route(method, path, handlerName)
Route a handler. Handlers can be routed at more than one location. Just like
express, you can also use this method directly on the controller (see example).
Parameters
method
. The http method, for example 'get'
, 'post'
, 'put'
, etc.path
. The path to route the handler to, in exactly the same format you would
pass to express. You can use a regex, but it will ignore options.prefix
.handlerName
. The name of the handler to route.
Example
users.route('get', '/user/:id', 'view');
users.route('post', '/user/:id', 'create');
users.route('put', '/user/:id', 'edit');
users.get('/user/:id', 'view')
users.post('/user/:id', 'create');
users.delete('/user/:id', 'delete');
### direct(method, path, [middleware\*/groups\*,] handlerfn)
Directly route a function optionally with some middleware. This is essentially
the same as adding a route directly to express. The difference is that handlers
defined with direct
can be included in the controller's middleware groups, and
will be included in the all
group.
Paramaters
method
. The http method, for example 'get'
, 'post'
, 'put'
, etc.path
. The path to route the handler to, in exactly the same format you would
pass to express. You can use a regex, but it will ignore options.prefix
.middleware/groups
. A bunch of middlewares or groups to add the route to.
These can be mixed and matched, Controller will figure it out.handlerfn
. The handler function to call when the route is visited.
Example
var uselessMiddleware = function(req,res,next) { next(); };
users.direct('delete', '/user/:id', uselessMiddleware, 'require-login', function(req, res) {
Users.delete(req.params.id);
res.end();
});
users.direct('get', '/user/do-something', function(req, res) {});