-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (60 loc) · 1.96 KB
/
index.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
var http = require('http'); //add the http module
var fs = require('fs'); //add the http module
var spriteFile;
var cssFile;
const filesMap = new Map();
['./images/awAchSprite.png',
'./images/board.jpg',
'./images/oceanTile.png',
'./images/resSprite.png',
'./main.js',
'./favicon.ico',
'./board.html',
'./index.html',
'./styles/main.css'].forEach(fileName =>
fs.readFile(fileName, function (err, data) {
if (err) {
throw err;
}
filesMap.set(fileName, data);
}));
var myServer = http.createServer(function (request, response) {
var awards, achievements;
let source = filesMap.get(`.${request.url}`);
switch (request.url) {
case "/main.js":
response.writeHead(200, { "Content-Type": "application/javascript" });
break;
case "/styles/main.css":
response.writeHead(200, { "Content-Type": "text/css" });
break;
case "/images/board.jpg":
case "/images/awAchSprite.png":
case "/images/resSprite.png":
case "/images/oceanTile.png":
if (!response.getHeader('Cache-Control') || !response.getHeader('Expires')) {
response.setHeader("Cache-Control", "public, max-age=345600"); // ex. 4 days in seconds.
response.setHeader("Expires", new Date(Date.now() + 345600000).toUTCString()); // in ms.
}
if(request.url == "/images/board.jpg") {
response.writeHead(200, { "Content-Type": "image/jpeg" });
}
else {
response.writeHead(200, { "Content-Type": "image/png" });
}
break;
case '/favicon.ico':
response.writeHead(200, { "Content-Type": "image/x-icon" });
break;
case '/board':
response.writeHead(200, { "Content-Type": "text/html" });
source = filesMap.get('./board.html');
break;
default:
response.writeHead(200, { "Content-Type": "text/html" });
source = filesMap.get('./index.html');
};
response.end(source);
});
var port = process.env.PORT || 3000
myServer.listen(port);