-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·72 lines (56 loc) · 2.16 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
// environments
var local_redis = process.platform == 'win32' ? '192.168.6.167' : '127.0.0.1';
var ENV = {
redis : { host: local_redis, port: 6379 }, // 192.168.6.131
db : 'mongodb://settlersOfAsia:comp361@ds033116.mlab.com:33116/settlers' // mlab.com
};
// dependencies
var port = Number(process.env.PORT || 3000);
var http = require('http');
var mongoose = require('mongoose');
var express = require('express');
var Session = require('express-session');
var bodyParser = require('body-parser');
var redis = require('redis');
var app = express();
var server = http.createServer(app);
var socketio = require('socket.io');
var ios = require('socket.io-express-session');
// create a redis client
global.MyRedis = redis.createClient(ENV.redis.port, ENV.redis.host);
MyRedis.on("error", function(err) {
console.error("REDIS ERROR:", err);
});
// use redis for session
var RedisStore = require('connect-redis')(Session);
var sessionConfig = Session({
store: new RedisStore({ client: MyRedis }),
key: 'jsessionid',
secret: 'secret sauce',
saveUninitialized: false, // don't create session until something stored; however, this will create a temporary session that last for the duration of the request
resave: false, // don't save session if unmodified
cookie: { secure: false, maxAge: 365*24*60*60*1000 }, // session expires in 1 year
rolling: true // any subsequential request resets the maxAge
});
// HTTP middlewares
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static(__dirname + '/public'));
app.use(sessionConfig);
// database
mongoose.connect(ENV.db);
// socket.io
var io = socketio(server);
var socketUser = require('./server/middleware/socketUser.js');
io.use(ios(sessionConfig));
io.use(socketUser); // access user object from socket.user
global.io = io;
// routes
require('./server/routes.js')(app); // file fetching
require('./server/rest.js')(app); // all ajax request
require('./server/game.js'); // game
// start the server
server.listen(port, function() {
console.log('SERVER STARTED ON PORT:', port);
console.log('PROCESS PID:', process.pid);
});