7 var express
= require('../../..');
8 var fs
= require('node:fs');
9 var path
= require('node:path');
11 module
.exports = function(parent
, options
){
12 var dir
= path
.join(__dirname
, '..', 'controllers');
13 var verbose
= options
.verbose
;
14 fs
.readdirSync(dir
).forEach(function(name
){
15 var file
= path
.join(dir
, name
)
16 if (!fs
.statSync(file
).isDirectory()) return;
17 verbose
&& console
.log('\n %s:', name
);
18 var obj
= require(file
);
19 var name
= obj
.name
|| name
;
20 var prefix
= obj
.prefix
|| '';
26 // allow specifying the view engine
27 if (obj
.engine
) app
.set('view engine', obj
.engine
);
28 app
.set('views', path
.join(__dirname
, '..', 'controllers', name
, 'views'));
30 // generate routes based
31 // on the exported methods
32 for (var key
in obj
) {
34 if (~['name', 'prefix', 'engine', 'before'].indexOf(key
)) continue;
39 url
= '/' + name
+ '/:' + name
+ '_id';
43 url
= '/' + name
+ 's';
47 url
= '/' + name
+ '/:' + name
+ '_id/edit';
51 url
= '/' + name
+ '/:' + name
+ '_id';
62 /* istanbul ignore next */
63 throw new Error('unrecognized route: ' + name
+ '.' + key
);
70 // before middleware support
72 app
[method
](url
, obj
.before
, handler
);
73 verbose
&& console
.log(' %s %s -> before -> %s', method
.toUpperCase(), url
, key
);
75 app
[method
](url
, handler
);
76 verbose
&& console
.log(' %s %s -> %s', method
.toUpperCase(), url
, key
);