This repository has been archived by the owner on Jun 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (47 loc) · 1.62 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
// index.js
const express = require('express');
const socketIO = require('socket.io-client');
const cors = require('cors');
const fs = require('fs')
require('./estimate');
const app = express();
const port = 9001;
app.use(cors('*'))
const socket = socketIO('https://dadlaughbutton.com/');
let laughterCount = 0;
let connectedCount = 0;
let lastLaughUpdate = 0
let lastConnectedUpdate = 0;
socket.on('getCount:laughs', function (v) {
laughterCount = v;
lastLaughUpdate = Date.now()
});
socket.on('getCount:connected', function (v) {
connectedCount = v;
lastConnectedUpdate = Date.now()
});
app.get('/uwu', (req, res) => {
res.status(200).json("uwu?")
})
app.get('*', (req, res) => {
try {
const parsedConnectedCount = parseInt(connectedCount.replaceAll(",", ""));
const parsedLaughterCount = parseInt(laughterCount.replaceAll(",", ""));
const parsedLastConnectedUpdate = parseInt(lastConnectedUpdate);
const parsedLastLaughUpdate = parseInt(lastLaughUpdate);
const responseObject = {
connectedCount: parsedConnectedCount,
laughterCount: parsedLaughterCount,
lastConnectedUpdate: parsedLastConnectedUpdate,
lastLaughUpdate: parsedLastLaughUpdate
};
res.status(200).json(responseObject);
} catch (error) {
console.error("Error occurred:", error);
const estimatedValue = parseInt(fs.readFileSync('estimatedValue.txt', 'utf8'))
res.status(500).json({ error: "Internal server error", estimatedValue });
}
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});