forked from cliftonc/express-mvc-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
141 lines (106 loc) · 3.29 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Module dependencies.
*/
var fs = require('fs'),express = require('express'),
mongoose = require('mongoose'), nodepath = require('path');
var path = __dirname;
var app;
/**
* Initial bootstrapping
*/
exports.boot = function(params){
//Create our express instance
app = express.createServer();
// Import configuration
require(path + '/conf/configuration.js')(app,express);
// Bootstrap application
bootApplication(app);
bootModels(app);
bootControllers(app);
return app;
};
/**
* App settings and middleware
* Any of these can be added into the by environment configuration files to
* enable modification by env.
*/
function bootApplication(app) {
// launch
// app.use(express.logger({ format: ':method :url :status' }));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ secret: 'helloworld' }));
app.use(express.static(path + '/public')); // Before router to enable dynamic routing
app.use(app.router);
// Example 500 page
app['error'](function(err, req, res){
console.log('Internal Server Error: ' + err.message);
res.render('500');
});
// Example 404 page via simple Connect middleware
app.use(function(req, res){
res.render('404');
});
// Setup ejs views as default, with .html as the extension
app.set('views', path + '/views');
app.register('.html', require('ejs'));
app.set('view engine', 'html');
// Some dynamic view helpers
app.dynamicHelpers({
request: function(req){
return req;
},
hasMessages: function(req){
return Object.keys(req.session.flash || {}).length;
},
messages: function(req){
return function(){
var msgs = req.flash();
console.log(msgs);
return Object.keys(msgs).reduce(function(arr, type){
return arr.concat(msgs[type]);
}, []);
}
}
});
}
//Bootstrap models
function bootModels(app) {
fs.readdir(path + '/models', function(err, files){
if (err) throw err;
files.forEach(function(file){
bootModel(app, file);
});
});
// Connect to mongoose
mongoose.connect(app.set('db-uri'));
}
// Bootstrap controllers
function bootControllers(app) {
fs.readdir(path + '/controllers', function(err, files){
if (err) throw err;
files.forEach(function(file){
// bootController(app, file);
});
});
require(path + '/controllers/AppController')(app); // Include
}
// simplistic model support
function bootModel(app, file) {
var name = file.replace('.js', ''),
schema = require(path + '/models/'+ name); // Include the mongoose file
}
// Load the controller, link to its view file from here
function bootController(app, file) {
var name = file.replace('.js', ''),
controller = path + '/controllers/' + name, // full controller to include
template = name.replace('Controller','').toLowerCase(); // template folder for html - remove the ...Controller part.
// Include the controller
// require(controller)(app,template); // Include
}
// allow normal node loading if appropriate
if (!module.parent) {
exports.boot().listen(3000);
console.log("Express server %s listening on port %d", express.version, app.address().port)
}