Skip to content
Myr

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 file

Take 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 back
1/**
2 * Module dependencies.3 */45var express = require('../../..');6var fs = require('fs');

examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae

line 7why, one step back
7var path = require('path');

4.13.1Douglas Christopher Wilsonbefore adoption2ac2509

lines 8–9why, one step back
8
9module.exports = function(parent, options){

examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae

line 10why, one step back
10 var dir = path.join(__dirname, '..', 'controllers');

4.13.1Douglas Christopher Wilsonbefore adoption2ac2509

line 11why, one step back
11 var verbose = options.verbose;

examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae

lines 12–14why, one step back
12 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 back
15 verbose && console.log('\n %s:', name);

examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae

line 16why, one step back
16 var obj = require(file);

4.13.1Douglas Christopher Wilsonbefore adoption2ac2509

lines 17–21why, one step back
17 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 back
22 var url;

4.13.1Douglas Christopher Wilsonbefore adoption2ac2509

lines 23–25why, one step back
23
24 // 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 back
26 app.set('views', path.join(__dirname, '..', 'controllers', name, 'views'));

4.13.1Douglas Christopher Wilsonbefore adoption2ac2509

lines 27–36why, one step back
27
28 // 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 back
37 url = '/' + name + '/:' + name + '_id';

4.13.1Douglas Christopher Wilsonbefore adoption2ac2509

lines 38–40why, one step back
38 break;
39 case 'list':40 method = 'get';

examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae

line 41why, one step back
41 url = '/' + name + 's';

4.13.1Douglas Christopher Wilsonbefore adoption2ac2509

lines 42–44why, one step back
42 break;
43 case 'edit':44 method = 'get';

examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae

line 45why, one step back
45 url = '/' + name + '/:' + name + '_id/edit';

4.13.1Douglas Christopher Wilsonbefore adoption2ac2509

lines 46–48why, one step back
46 break;
47 case 'update':48 method = 'put';

examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae

line 49why, one step back
49 url = '/' + name + '/:' + name + '_id';

4.13.1Douglas Christopher Wilsonbefore adoption2ac2509

lines 50–52why, one step back
50 break;
51 case 'create':52 method = 'post';

examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae

line 53why, one step back
53 url = '/' + name;

4.13.1Douglas Christopher Wilsonbefore adoption2ac2509

lines 54–56why, one step back
54 break;
55 case 'index':56 method = 'get';

examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae

line 57why, one step back
57 url = '/';

4.13.1Douglas Christopher Wilsonbefore adoption2ac2509

lines 58–65why, one step back
58 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 back
66 url = prefix + url;

4.13.1Douglas Christopher Wilsonbefore adoption2ac2509

lines 67–69why, one step back
67
68 // before middleware support69 if (obj.before) {

examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae

lines 70–71why, one step back
70 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 back
72 } else {

examples: use path.join instead of concatenationchainhelenbefore adoption8eb95ae

lines 73–74why, one step back
73 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 back
75 }
76 }7778 // mount the app79 parent.use(app);80 });81};