-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
44 lines (39 loc) · 1.1 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
let http = require('http'),
path = require('path'),
fs = require('fs'),
game = require('./scripts/server/game');
let mimeTypes = {
'.js' : 'text/javascript',
'.html' : 'text/html',
'.css' : 'text/css',
'.png' : 'image/png',
'.jpg' : 'image/jpeg',
'.wav' : 'audio/wav',
'.ico' : 'image/x-icon'
};
function handleRequest(request, response) {
let lookup = (request.url === '/') ? '/index.html' : decodeURI(request.url);
let file = lookup.substring(1, lookup.length);
fs.exists(file, function(exists) {
if (exists) {
fs.readFile(file, function(err, data) {
if (err) {
response.writeHead(500);
response.end('Server Error!');
} else {
let headers = {'Content-type': mimeTypes[path.extname(lookup)]};
response.writeHead(200, headers);
response.end(data);
}
});
} else {
response.writeHead(404);
response.end();
}
});
}
let server = http.createServer(handleRequest);
server.listen(3000, function() {
game.initialize(server);
console.log('Server is listening on port 3000');
});