This repository has been archived by the owner on Dec 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
69 lines (56 loc) · 1.53 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
var fs = require('fs');
var express = require('express');
var exphbs = require('express-handlebars');
var postData = require('./postData.json');
var leaderBoardData = require('./leaderBoardData.json');
var app = express();
var port = process.env.PORT || 3000;
app.engine('handlebars', exphbs.engine({ defaultLayout: 'main' }));
app.set('view engine', 'handlebars');
app.use(express.json());
app.use(express.static('public'));
app.get('/', function(req, res) {
res.status(200).render('mainPage', { postData: postData });
});
app.get('/leaderboard', function(req, res) {
res.status(200).render('leaderBoardPage', {
leaderBoardData: leaderBoardData
});
});
app.get('/getMinScore', function(req, res) {
var c = leaderBoardData.length;
if (c > 0) {
res.json({
count: c,
score: leaderBoardData[c - 1].score
});
} else {
res.json({ count: c });
}
});
app.post('/addScore', function (req, res, next) {
leaderBoardData.push({
name:req.body.name,
score:req.body.score,
date:req.body.date
});
leaderBoardData.sort((a, b) => b.score - a.score);
leaderBoardData = leaderBoardData.slice(0, 10);
fs.writeFile('./leaderBoardData.json',
JSON.stringify(leaderBoardData, null, 2),
function (err) {
if (err) {
res.status(500).send("Failed to upload score.");
} else {
res.status(200).send("Score successfully uploaded.");
}
}
);
});
app.get('*', function (req, res) {
res.status(404).render('404');
});
app.listen(port, function (err) {
if(err) throw err;
console.log('== Server is listening on port', port);
});