fix: added a missing semicolon in css styles in examples/auth (#6297)
[express.git] / examples / mvc / lib / boot.js
blobfc2ab0fad9974e9f8916cedd85b08cb20b77dcb8
1 'use strict'
3 /**
4 * Module dependencies.
5 */
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 || '';
21 var app = express();
22 var handler;
23 var method;
24 var url;
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) {
33 // "reserved" exports
34 if (~['name', 'prefix', 'engine', 'before'].indexOf(key)) continue;
35 // route exports
36 switch (key) {
37 case 'show':
38 method = 'get';
39 url = '/' + name + '/:' + name + '_id';
40 break;
41 case 'list':
42 method = 'get';
43 url = '/' + name + 's';
44 break;
45 case 'edit':
46 method = 'get';
47 url = '/' + name + '/:' + name + '_id/edit';
48 break;
49 case 'update':
50 method = 'put';
51 url = '/' + name + '/:' + name + '_id';
52 break;
53 case 'create':
54 method = 'post';
55 url = '/' + name;
56 break;
57 case 'index':
58 method = 'get';
59 url = '/';
60 break;
61 default:
62 /* istanbul ignore next */
63 throw new Error('unrecognized route: ' + name + '.' + key);
66 // setup
67 handler = obj[key];
68 url = prefix + url;
70 // before middleware support
71 if (obj.before) {
72 app[method](url, obj.before, handler);
73 verbose && console.log(' %s %s -> before -> %s', method.toUpperCase(), url, key);
74 } else {
75 app[method](url, handler);
76 verbose && console.log(' %s %s -> %s', method.toUpperCase(), url, key);
80 // mount the app
81 parent.use(app);
82 });