boot.js
This file
Path
examples/mvc/lib/boot.js
81 lines · 2.2 KiB · JavaScript
Last changed by
examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae
History of this fileTake it
The walk stopped after 200 commits: lines older than 2ac2509 point at it.
4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
lines 1–6why, one step back1/**2 * Module dependencies.3 */45var express = require('../../..');6var fs = require('fs');examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae
line 7why, one step back7var path = require('path');4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
lines 8–9why, one step back89module.exports = function(parent, options){examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae
line 10why, one step back10 var dir = path.join(__dirname, '..', 'controllers');4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
line 11why, one step back11 var verbose = options.verbose;examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae
lines 12–14why, one step back12 fs.readdirSync(dir).forEach(function(name){13 var file = path.join(dir, name)14 if (!fs.statSync(file).isDirectory()) return;4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
line 15why, one step back15 verbose && console.log('\n %s:', name);examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae
line 16why, one step back16 var obj = require(file);4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
lines 17–21why, one step back17 var name = obj.name || name;18 var prefix = obj.prefix || '';19 var app = express();20 var handler;21 var method;examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae
line 22why, one step back22 var url;4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
lines 23–25why, one step back2324 // allow specifying the view engine25 if (obj.engine) app.set('view engine', obj.engine);examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae
line 26why, one step back26 app.set('views', path.join(__dirname, '..', 'controllers', name, 'views'));4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
lines 27–36why, one step back2728 // generate routes based29 // on the exported methods30 for (var key in obj) {31 // "reserved" exports32 if (~['name', 'prefix', 'engine', 'before'].indexOf(key)) continue;33 // route exports34 switch (key) {35 case 'show':36 method = 'get';examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae
line 37why, one step back37 url = '/' + name + '/:' + name + '_id';4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
lines 38–40why, one step back38 break;39 case 'list':40 method = 'get';examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae
line 41why, one step back41 url = '/' + name + 's';4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
lines 42–44why, one step back42 break;43 case 'edit':44 method = 'get';examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae
line 45why, one step back45 url = '/' + name + '/:' + name + '_id/edit';4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
lines 46–48why, one step back46 break;47 case 'update':48 method = 'put';examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae
line 49why, one step back49 url = '/' + name + '/:' + name + '_id';4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
lines 50–52why, one step back50 break;51 case 'create':52 method = 'post';examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae
line 53why, one step back53 url = '/' + name;4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
lines 54–56why, one step back54 break;55 case 'index':56 method = 'get';examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae
line 57why, one step back57 url = '/';4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
lines 58–65why, one step back58 break;59 default:60 /* istanbul ignore next */61 throw new Error('unrecognized route: ' + name + '.' + key);62 }6364 // setup65 handler = obj[key];examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae
line 66why, one step back66 url = prefix + url;4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
lines 67–69why, one step back6768 // before middleware support69 if (obj.before) {examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae
lines 70–71why, one step back70 app[method](url, obj.before, handler);71 verbose && console.log(' %s %s -> before -> %s', method.toUpperCase(), url, key);4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
line 72why, one step back72 } else {examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae
lines 73–74why, one step back73 app[method](url, handler);74 verbose && console.log(' %s %s -> %s', method.toUpperCase(), url, key);4.13.1Douglas Christopher Wilsonbefore adoption2ac2509
lines 75–81why, one step back75 }76 }7778 // mount the app79 parent.use(app);80 });81};