-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathserver.js
115 lines (91 loc) · 2.54 KB
/
server.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
var app,
app_root = __dirname,
app_port = process.env.PORT || 1337,
env = require('./environment.json').environment,
dbConfig = require('./db/config.js')[env],
fs = require('fs'),
express = require('express'),
util = require('util'),
mongoose = require('mongoose'),
logging = require('node-logging');
// Connect to the Mongo db
mongoose.connect(dbConfig.db);
mongoose.connection.once('open', function () {
console.log('Connected successfully....');
});
mongoose.connection.once('error', function () {
console.error('MONGO SERVER NOT STARTED!');
});
// Initial bootstrapping
exports.boot = function(params){
//Create our express instance
app = express();
// Bootstrap application
bootApplication(app);
bootModels();
bootControllers();
return app;
};
function bootApplication(app) {
// Config
app.engine('html', require('ejs').renderFile);
// Middleware
app.use(express.bodyParser());
app.use(express.compress());
app.use(express.methodOverride());
app.use(express.cookieParser());
// Serve static files from the /public directory
app.use('/', express.static(app_root + '/public'));
// Set up an expressJs session
app.use(express.session({
secret: 'SUPERimportantSTRING CHANGE IT',
maxAge: new Date(Date.now() + 3600000)
}));
// Moves our routes above the Middleware
// so this will attempt to match our route before continueing
app.use(app.router);
// Set up logging here so you dont get a log for every static file
app.use(express.logger());
app.use(logging.requestLogger);
}
//Bootstrap models
function bootModels() {
fs.readdir(app_root + '/db/models', function (err, files) {
if (err) {
throw err;
}
files.forEach(function (file) {
bootModel(file);
});
});
}
// Bootstrap controllers
function bootControllers() {
fs.readdir(app_root + '/db/controllers', function(err, files){
if (err) {
throw err;
}
files.forEach(function(file){
bootController(file);
});
});
// Include the main Controller
require(app_root + '/db/controllers/AppController')(app);
}
function bootModel(file) {
var name = file.replace('.js', '');
// Include the mongoose file
require(app_root + '/db/models/'+ name);
}
// Load the controller, link to its view file from here
function bootController(file) {
var name = file.replace('.js', ''),
controller = app_root + '/db/controllers/' + name;
// Include the controller
require(controller);
}
// allow normal node loading if appropriate
if (!module.parent) {
exports.boot().listen(app_port);
console.log('Express server listening on port %d', app_port);
}