-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver1.js
166 lines (135 loc) · 4.11 KB
/
server1.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var app,
app_root = __dirname,
app_port = 3000,
fs = require('fs'),
express = require('express'),
util = require('util'),
SessionMongoose = require("session-mongoose"),
mongoose = require('mongoose'),
logging = require('node-logging'),
stylus = require('stylus'),
nib = require('nib'),
passport = require('passport');
var mongooseSessionStore = new SessionMongoose({
url: "mongodb://localhost/session",
interval: 6000 // expiration check worker run interval in millisec (default: 60000)
});
mongoose.connect('mongodb://localhost/boilerplate');
mongoose.connection.on('error', console.error);
// Initial bootstrapping
exports.boot = function(params){
//Create our express instance
app = express.createServer();
// Import configuration
require(app_root + '/conf/configuration.js')(app,express);
// Bootstrap application
bootApplication(app);
bootModels(app);
bootControllers(app);
bootFaye(app);
return app;
};
function compileStylus (str, path) {
return stylus(str)
.set('filename', path)
.set('compress', false)
.use(nib());
}
function bootApplication(app) {
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
// before sessions to prevent passport.deserializeUser being called for static assets
app.use(express.static(app_root + '/public_app'));
app.use(express.session({
secret: "changeSecret",
maxAge: new Date(Date.now() + 3600000),
store: mongooseSessionStore
}));
app.use(passport.initialize());
app.use(passport.session());
// dynamic router
app.use(app.router);
app.use(express.logger());
app.use(logging.requestLogger);
// 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', app_root + '/views');
app.register('.html', require('ejs'));
app.set('view engine', 'html');
// Some dynamic view helpers
app.dynamicHelpers({
request: function(req){ return req; },
//user: function(req){ return req.user; },
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]);
}, []);
}
}
});
}
function compile(str, path) {
return stylus(str)
.set('filename', path)
.set('compress', true)
.use(nib());
}
//Bootstrap models
function bootModels(app) {
fs.readdir(app_root + '/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(app_root + '/controllers', function(err, files){
if (err) throw err;
files.forEach(function(file){
bootController(app, file);
});
});
require(app_root + '/controllers/AppController')(app); // Include
}
function bootModel(app, file) {
var name = file.replace('.js', ''),
schema = require(app_root + '/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 = app_root + '/controllers/' + name, // full controller to include
template = name.replace('Controller','').toLowerCase(); // template folder for html - remove the ...Controller part.
// console.log('bootController')
// console.log('name = '+name)
// console.log('controller = '+controller)
// console.log('template = '+template)
// Include the controller
//require(controller)(app,template); // Include
}
function bootFaye(app) {
var bayeux = require(app_root + '/utils/faye');
bayeux.attach(app);
}
// allow normal node loading if appropriate
if (!module.parent) {
exports.boot().listen(app_port);
console.log("Express server %s listening on port %d", express.version, app.address().port)
}