This repository has been archived by the owner on Dec 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
129 lines (118 loc) · 3.68 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const express = require("express");
const redis = require("redis");
const app = express();
const port = 3000;
const client = redis.createClient({
url: process.env.REDIS_URL ?? "redis://localhost:6379"
});
client.on("error", (error) => {
console.error("Redis connection error:", error);
});
client.connect();
client.on("connect", () => {
console.log("Redis client connected");
});
// Serve the front-end to the client
app.get("/", async (req, res) => {
try {
const reply = await client.get("counter");
const counter = reply || 0;
res.send(`
<html>
<head>
<title>Front-end</title>
<style>
body {
background-color: #252525;
color: #fff;
font-family: Arial, sans-serif;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
}
.container {
text-align: center;
}
h1 {
margin-bottom: 16px;
}
p {
margin-bottom: 8px;
}
.button {
display: inline-block;
padding: 8px 16px;
background-color: #4caf50;
color: white;
text-decoration: none;
border-radius: 4px;
cursor: pointer;
border: none;
outline: none;
}
.button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="container">
<h1>Ergomake Example</h1>
<p>Counter: <span id="counter">${counter}</span></p>
<button class="button" onclick="increment()">Increment</button>
<button class="button" onclick="decrement()">Decrement</button>
</div>
<script>
function updateCounter(value) {
document.getElementById('counter').textContent = value;
}
function increment() {
fetch('/increment')
.then(response => response.text())
.then(message => {
updateCounter(message);
})
.catch(error => console.log(error));
}
function decrement() {
fetch('/decrement')
.then(response => response.text())
.then(message => {
updateCounter(message);
})
.catch(error => console.log(error));
}
</script>
</body>
</html>
`);
} catch (err) {
console.error("Redis GET error:", err);
res.status(500).send("Internal Server Error");
}
});
// Increment the counter on each request to /increment endpoint
app.get("/increment", async (req, res) => {
try {
const reply = await client.incr("counter");
res.send(reply.toString());
} catch (err) {
console.error("Redis INCR error:", err);
res.status(500).send("Internal Server Error");
}
});
// Decrement the counter on each request to /decrement endpoint
app.get("/decrement", async (req, res) => {
try {
const reply = await client.decr("counter");
res.send(reply.toString());
} catch (err) {
console.error("Redis DECR error:", err);
res.status(500).send("Internal Server Error");
}
});
app.listen(port, () => {
console.log(`Express server listening on port ${port}`);
});