forked from pianosaurus/awesomeBoard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
67 lines (55 loc) · 1.71 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
'use strict';
/////////////////////
// Package imports //
/////////////////////
var express = require('express');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var compression = require('compression');
var mongoose = require('mongoose');
var passport = require('passport');
var morgan = require('morgan');
var MongoStore = require('connect-mongo')(session);
// Configuration
var configDB = require('./config/database');
var configServer = require('./config/server');
///////////
// Setup //
///////////
// Initialise server and set port.
var app = express();
// Compress all requests.
app.use(compression());
// Connect to MongoDB.
mongoose.connect(configDB.url);
// Set up the express application.
app.use(morgan('dev')); // Console logging
app.use(cookieParser());
app.use(bodyParser.urlencoded({ // Parse application/x-www-form-urlencoded
extended: false
}));
app.use(bodyParser.json()); // Parse application/json
// Login/session setup.
app.use(session({
resave: false,
saveUninitialized: false,
secret: configServer.sessionSecret,
store: new MongoStore({
mongooseConnection: mongoose.connection
})
}));
require('./app/passport')(app, passport);
// Routes.
app.use(express.static(__dirname + '/public'));
require('./app/routes')(app, passport, configServer);
// Set up sockets and events.
var server = require('http').Server(app);
var io = require('socket.io')(server);
require('./app/sockets')(io);
////////////
// Launch //
////////////
var port = configServer.listenPort;
server.listen(port); // Must use server.listen instead of app.listen for socket.IO to work.
console.log(configServer.title + ' is listening on port ' + port + '.');