-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp.js
128 lines (105 loc) · 2.84 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
var express = require('express')
, http = require('http')
, path = require('path')
, mongoose = require('mongoose')
, fs = require('fs');
exports.boot = function(callback)
{
var app = express();
var startup = [
BootApplication,
BootModels,
BootControllers
];
function next() {
var step = startup.shift();
if(step)
step(app, next);
else
callback(app);
}
next();
return app;
}
// Setup any server configurations
function BootApplication(app, next)
{
// all environments
app.set('port', process.env.PORT || 3000);
app.use("/public", express.static(path.join(__dirname, 'public')));
app.use('/Views/Templates', express.static(path.join(__dirname, 'Views/Templates')));
app.set('views', __dirname + '/Views');
app.set('view engine', 'jade');
app.engine('jade', require('jade').__express);
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('legacybass'));
app.use(express.session());
app.configure('development', function()
{
app.set('db-uri', 'mongodb://localhost/jeopardy-dev');
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function()
{
app.set('db-uri', 'mongodb://localhost/jeopardy');
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('test', function()
{
app.set('db-uri', 'mongodb://localhost/jeopardy-test');
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
next();
}
// Load the controllers into the routing domain
function BootControllers(app, next)
{
// routing
var homeController = require('./Controllers/HomeController').default;
var apiController = require('./Controllers/ApiController').default;
app.use(app.router);
apiController(app);
homeController(app);
next();
}
// Load the models into the mongoose framework
function BootModels(app, next)
{
fs.readdir('./Schemas', function(err, files)
{
if(err)
throw err;
files.forEach(function(file)
{
var name = file.replace(".js", ""),
schema = require("./Schemas/" + name);
mongoose.model(name, schema);
});
mongoose.connect(app.get('db-uri'));
var db = mongoose.connection;
db.on('error', function() {
var message = ['connection error:'];
Array.prototype.map.call(arguments, function(item) { message.push(item); });
console.error.apply(console, message);
});
db.once('open', function()
{
next();
});
});
}
// Load the socket configurations and handlers
function BootSockets(server)
{
require('./Controllers/SocketController').default(server);
}
var app = exports.boot(function(app) {
var server = http.createServer(app);
server.listen(app.get('port'), function(){
console.log('Express server listening on port %d.', app.get('port'));
});
BootSockets(server);
});