-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
96 lines (84 loc) · 2.7 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
if(process.env.NODETIME_ACCOUNT_KEY) {
require('nodetime').profile({
accountKey: process.env.NODETIME_ACCOUNT_KEY,
appName: 'zwinkapp'
});
}
var application_root = __dirname,
express = require("express"),
expressValidator = require('express-validator'),
mongoose = require('mongoose'),
passport = require('passport'),
jade = require('jade'),
flash = require('connect-flash'),
http = require('http'),
socketIo = require("socket.io"),
LocalStrategy = require('passport-local').Strategy;
var app = express(),
server = http.createServer(app),
io = socketIo.listen(server);
// Configure server
app.configure(function() {
app.set('views', __dirname + '/site/views');
app.set('view engine', 'jade');
app.use(express.static(__dirname + '/site'));
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.session({ cookie: {maxAge: 86400000 }, secret: "keyboard cat"}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use(expressValidator());
// app.use(express.logger());
app.use(express.methodOverride());
app.use(app.router);
app.locals.pretty = true;
//Show all errors in development
// app.use(express.errorHandler({dumpExceptions: true, showStack: true}));
});
// MongoDB
var mongoUri = process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://localhost/zwink';
mongoose.connect(mongoUri, function (err, res) {
if (err) {
console.log ('ERROR connecting to: ' + mongoUri + '. ' + err);
} else {
console.log ('Succeeded connection to: ' + mongoUri);
}
});
var UserModel = require('./model/user.js');
// Setup authentification
passport.use(new LocalStrategy(UserModel.authenticate()));
passport.serializeUser(UserModel.serializeUser());
passport.deserializeUser(UserModel.deserializeUser());
// Setup routes
require("./routes")(app);
// Setup chat
require("./chat")(io);
function startKeepAlive() {
setInterval(function() {
var options = {
host: 'zwinkapp.herokuapp.com',
port: 80,
path: '/'
};
http.get(options, function(res) {
res.on('data', function(chunk) {
try {
// optional logging... disable after it's working
// console.log("HEROKU RESPONSE: " + chunk);
} catch (err) {
console.log(err.message);
}
});
}).on('error', function(err) {
console.log("Error: " + err.message);
});
}, 60 * 30 * 1000); // load every 30 minutes
}
startKeepAlive();
var port = process.env.PORT || 5000;
server.listen(port, function() {
console.log("Listening on " + port);
});