-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
80 lines (69 loc) · 1.79 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
/* global app */
// server.js
// where your node app starts
// init
var H = require("hyperweb");
var hs = require("./highscores");
app = H.blastOff();
app.get("/", function (request, response) {
try {
response.render('index.html', {});
} catch (err) {
handleError(err, response);
}
});
app.get("/highscore", function (request, response) {
try {
var ixNew = -1;
var scores = hs.getScores().slice();
if (request.query.score) {
var newScore = {
name: "",
level: parseInt(request.query.level),
score: parseInt(request.query.score),
};
if (newScore.score > 0) {
ixNew = hs.insertScore(scores, newScore);
}
}
response.render('highscores.html', {scores: scores, ixNew: ixNew});
} catch (err) {
handleError(err, response);
}
});
app.get("/highscore/raw", function (request, response) {
try {
response.send(JSON.stringify(hs.getScores()));
} catch (err) {
handleError(err, response);
}
});
app.post("/highscore", function (request, response) {
try {
var scores = hs.getScores();
var newScore = request.body;
newScore.score = parseInt(newScore.score);
newScore.level = parseInt(newScore.level);
hs.insertScore(scores, newScore);
hs.save();
response.redirect("/highscore");
} catch (err) {
handleError(err, response);
}
});
// app.get("/highscore/delete", function (request, response) {
// try {
// hs.getScores().splice(0);
// hs.save();
// response.send("Success");
// } catch (err) {
// handleError(err, response);
// }
// });
function handleError(err, response) {
response.status(500);
response.send(
"<html><head><title>Internal Server Error!</title></head><body><pre>"
+ JSON.stringify(err, null, 2) + "</pre></body></pre>"
);
}