-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
89 lines (73 loc) · 2.28 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
/*
* Entry of the web application
*/
// Set up Express.js
var express = require('express');
var app = express();
var http = require('http').Server(app);
var envVars = require('./env_vars.js')
// Load libraries
var bodyParser = require('body-parser');
var morgan = require('morgan');
var favicon = require('serve-favicon');
var methodOverride = require('method-override');
var session = require('express-session');
var io = require('socket.io')(http);
// Set up logging
app.use(morgan('dev'));
// Set up body parsing
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Set up static content
app.use(express.static(__dirname + '/node_modules')); // client-side frameworks
app.use(express.static(__dirname + '/public')); // HTML, CSS
// Set up favicon
app.use(favicon(__dirname + '/public/favicon.ico'));
// passport-local-auth
var mongoose = require('mongoose');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
mongoose.Promise = require('bluebird');
app.use(methodOverride());
app.use(session({
resave: false,
saveUninitialized: false,
secret: envVars.secret
}));
app.use(passport.initialize());
app.use(passport.session());
var User = require('./models/User.js');
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
// Connect to Mongodb
require('./config/db')()
// Set up app routes
require('./config/routes')(app, io, passport);
// TODO: Set up CSURF, maybe?
// Default error handler
app.use(function(err, req, res, next) {
console.error(err.stack);
switch (err.name) {
case "ValidationError":
case "CastError":
case "AuthError":
err.status = 400;
break;
// Add further specific errors here
}
res.status(err.status || 500).send({ error: err });
});
exports = module.exports = app;
if (!module.parent) {
var port = process.env.PORT || 8080; // 8080 as default
io.on('connection', function(socket) {
socket.on('room', function(room) {
socket.join(room);
});
})
// On Linux make sure you have root to open port 80
http.listen(port, function() {
console.log('Listening on port ' + port);
});
}