express-enrouten
Route configuration middleware for expressjs.
API
app.use(enrouten(options))
var express = require('express'),
enrouten = require('express-enrouten');
var app = express();
app.use(enrouten({ ... });
Deprecated: enrouten(app).withRoutes(options)
var express = require('express'),
enrouten = require('express-enrouten');
Configuration
express-enrouten supports routes via configuration and convention.
app.use(enrouten({
method: 'get',
path: '/foo',
handler: function (req, res) {
}
}]
});
directory
(optional) - String or array of path segments. Specify a directory to have enrouten scan all files recursively
to find files that match the controller-spec API.
app.use(enrouten({
directory: 'controllers'
});
routes
(optional) An array of route definition objects. Each definition must have a path
and handler
property and
can have an optional method
property (method
defaults to 'GET').
app.use(enrouten({
routes: [
{ path: '/', method: 'GET', handler: require('./controllers/index') },
{ path: '/foo', method: 'GET', handler: require('./controllers/foo') }
]
});
index
(optional, overrides directory
and disables scanning) - String path or array of path segments indicating
the file to load which acts as the route 'index' of the application.
module.exports = function (app) {
app.get('/', index);
app.get('/account', passport.protect, account);
};
Controller Files
A 'controller' is defined as any require
-able file which exports a function that accepts a single argument. Any files with an extension of .js
(or .coffee
if CoffeeScript is registered) will be loaded and if it exports a function that accepts a single argument then this function will be called. NOTE: Any file in the directory tree that matches the API will be invoked/initialized with the express application object.
module.exports = function (app) {
app.get('/', function (req, res) {
});
};
exports = function (app) {
};
modules.exports = function (config) {
};
module.exports = {
importantHelper: function () {
}
};