-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
88 lines (62 loc) · 1.93 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
81
82
83
84
85
86
87
88
const express = require('express');
const app = express();
const sqlite3 = require('sqlite3').verbose();
const https = require('https');
setInterval(() => {
console.log("ping");
https.get("https://tetris-node.fly.dev");
}, 3 * 60 * 1000);
setInterval(() => {
console.log("ping2");
https.get("https://rent-a-bike.fly.dev");
}, 4 * 60 * 1000);
setInterval(() => {
console.log("ping3");
https.get("https://plain-scanner.fly.dev");
}, 4 * 60 * 1000);
// setInterval(() => {
// console.log("ping3");
// https.get("https://medicalappointmentapp.fly.dev");
// }, 2 * 60 * 1000);
const db = new sqlite3.Database("production.sqlite");
// const db = new sqlite3.Database("/mnt/highscores/production.sqlite");
// Create a highscores table if it doesn't exist
db.serialize(() => {
db.run('CREATE TABLE IF NOT EXISTS highscores (id INTEGER PRIMARY KEY AUTOINCREMENT, playerName TEXT, score INTEGER)');
});
app.use(express.json());
app.use(express.static('public'));
// Serve the index.html file
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
// Handle high score submission
app.post('/highscore', (req, res) => {
const highscore = req.body;
const query = 'INSERT INTO highscores (playerName, score) VALUES (?, ?)';
db.run(query, [highscore.playerName, highscore.score], (err) => {
if (err) {
console.error(err);
res.status(500).send('Error saving high score');
} else {
res.status(200).send('High score saved successfully');
}
});
});
// Retrieve highscores
app.get('/highscores', (req, res) => {
const query = 'SELECT * FROM highscores ORDER BY score DESC';
db.all(query, (err, rows) => {
if (err) {
console.error(err);
res.status(500).send('Error retrieving high scores');
} else {
res.status(200).json(rows);
}
});
});
// Start the server
const port = 8080;
app.listen(port, () => {
console.log('Server is running on port 8080');
});