-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
77 lines (66 loc) · 1.92 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
// This code was adapted from code originally written by Dr. Dean Mathias
const http = require('http');
const path = require('path');
const fs = require('fs');
let game = require('./server/game');
let lobby = require('./server/lobby');
let Users = require('./models/Users');
const { API } = require('./API');
let mimeTypes = {
'.js': 'text/javascript',
'.html': 'text/html',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.mp3': 'audio/mpeg3',
'.map': 'application/json',
'.wav': 'audio/wav',
'.ttf': 'font/ttf',
'.woff': 'font/woff',
'.woff2': 'font/woff2',
'.json': 'application/json'
};
function handleRequest(req, res) {
if(/\/api.*/.test(req.url)) {
// route begins with /api
API(req, res);
return;
}
let lookup = (req.url === '/') ? '/index.html' : decodeURI(req.url);
let file = lookup.substring(1, lookup.length);
file = path.join(__dirname, 'client_files', file);
fs.exists(file, function(exists) {
// TODO: Prevent client from accessing server code
if (exists) {
fs.readFile(file, function(err, data) {
if (err) {
res.writeHead(500);
res.end('Internal Server Error');
}
else {
let headers = {'Content-type': mimeTypes[path.extname(lookup)]};
res.writeHead(200, headers);
res.end(data);
}
});
}
else {
res.writeHead(404);
res.end();
}
});
}
let server = http.createServer(handleRequest);
const port = process.env.PORT || 3000;
server.listen(port, function() {
Users.load();
// Initialize root socket.io
const io = require('socket.io')(server);
// Create socket.io namespaces for the lobby and the game
const lobbyIO = io.of('/lobby');
const gameIO = io.of('/game');
// Initialize socket.io in both namespaces
lobby.initializeSocketIO(lobbyIO);
game.initializeSocketIO(gameIO);
console.log(`Server is listening on port ${port}`);
});