5 var express = require('../../..');
6 var fs = require('fs');
7 var path = require('path');
9 module.exports = function(parent, options){
10 var dir = path.join(__dirname, '..', 'controllers');
11 var verbose = options.verbose;
12 fs.readdirSync(dir).forEach(function(name){
13 var file = path.join(dir, name)
14 if (!fs.statSync(file).isDirectory()) return;
15 verbose && console.log('\n %s:', name);
16 var obj = require(file);
17 var name = obj.name || name;
18 var prefix = obj.prefix || '';
24 // allow specifying the view engine
25 if (obj.engine) app.set('view engine', obj.engine);
26 app.set('views', path.join(__dirname, '..', 'controllers', name, 'views'));
28 // generate routes based
29 // on the exported methods
30 for (var key in obj) {
32 if (~['name', 'prefix', 'engine', 'before'].indexOf(key)) continue;
37 url = '/' + name + '/:' + name + '_id';
41 url = '/' + name + 's';
45 url = '/' + name + '/:' + name + '_id/edit';
49 url = '/' + name + '/:' + name + '_id';
60 /* istanbul ignore next */
61 throw new Error('unrecognized route: ' + name + '.' + key);
68 // before middleware support
70 app[method](url, obj.before, handler);
71 verbose && console.log(' %s %s -> before -> %s', method.toUpperCase(), url, key);
73 app[method](url, handler);
74 verbose && console.log(' %s %s -> %s', method.toUpperCase(), url, key);