-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapp.js
140 lines (106 loc) · 3.55 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 express = require('express');
//http server
var http = require('http');
//Server path utils
var path = require('path');
//app icon
var favicon = require('serve-favicon');
// express related logging
var logger = require('morgan');
//Parse Cookie header and populate req.cookies
var cookieParser = require('cookie-parser');
//Parse request bodies
var bodyParser = require('body-parser');
//Provides faux HTTP method
var methodOverride = require('method-override');
//Development error handler
var errorHandler = require('errorhandler');
// mongodb OM
var mongoose = require('mongoose');
//user auth
var passport = require('passport');
//session flash messages
var flash = require('connect-flash');
// get the environment specific configuration
var CONFIG = require('config').travelapp;
// Setup Database config for mongoose
var configDB = require('./config/userDb.js');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
// Openshift Mongo DB env variable check
var connection = process.env.OPENSHIFT_MONGODB_DB_URL || CONFIG.dbUrl;
// configuration ===============================================================
var app = express();
var server = http.createServer(app);
//initialize socket.io
var io = require('socket.io').listen(server);
var allowCrossDomain = function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
};
require('./config/passport')(passport); // pass passport for configuration
var port = process.env.OPENSHIFT_NODEJS_PORT || CONFIG.port,
ip = process.env.OPENSHIFT_NODEJS_IP || CONFIG.appHost;
// all environments
app.set('port', port);
app.set('ipaddress', ip);
app.set('views', __dirname + CONFIG.viewDir);
app.set('view engine', 'jade');
app.use(favicon(__dirname + '/public/img/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(methodOverride());
app.use(express.static(path.join(__dirname, CONFIG.publicFolder))); // location of the files
//Handle Errors gracefully
app.use(function(err, req, res, next) {
if (!err) {
return next();
}
console.log(err.stack);
res.json({
error: true
});
});
// development only
if ('development' === app.get('env')) {
app.use(errorHandler());
app.use(allowCrossDomain);
}
mongoose.connect(configDB.dbUrl, configDB.options, function(e) {
// If error connecting
if (e) {
throw e;
}
// required for passport
app.use(session({
secret: CONFIG.secret, // session secret
store: new MongoStore({
url: configDB.dbUrl,
maxAge: CONFIG.cookie_max_age,
clear_interval: 3600,
auto_reconnect: true
}),
cookie: {
maxAge: CONFIG.cookie_max_age
// one week
}
}));
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
require('./routes/routes.js')(app, passport); // load our routes and pass in
// our app and fully configured
// passport
var poll_routes = require('./routes/poll_routes');
// listen on connection event using callback method of vote
io.sockets.on('connection', poll_routes.vote);
server.listen(port, ip, function() {
console.log('Express server listening on port ' + port + " ip:" + ip);
});
}); // connect to our database