-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
73 lines (59 loc) · 1.65 KB
/
server.js
File metadata and controls
73 lines (59 loc) · 1.65 KB
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
import 'dotenv/config';
import express from "express";
import mariadb from "mariadb";
import morgan from "morgan";
import { fileURLToPath } from "url";
const app = express();
const pool = mariadb.createPool({
host: process.env.DBHOST || "localhost",
user: process.env.DBUSER,
password: process.env.DBPASS,
database: process.env.DBNAME,
connectionLimit: 5,
});
app.use(morgan("combined"));
app.use(express.static(fileURLToPath(new URL("./public", import.meta.url))));
app.get("/hi", (req, res) => {
res.set("Content-Type", "text/plain");
res.send("hi!");
});
app.use(async (req, res, next) => {
const conn = await pool.getConnection();
req.conn = conn;
res.on("finish", () => {
conn.end();
});
next();
});
app.get("/num", async (req, res) => {
const rows = await req.conn.query("SELECT value FROM counter WHERE id = 1;");
res.json({ counter: rows[0]?.value });
});
app.post("/num", async (req, res) => {
const match = /^([+-]?\d+)$/.exec(req.query?.n);
if (!match) {
console.log(req.query);
res.status(400).json({ error: "param" });
return;
}
const num = Number(match[1]);
await req.conn.query("UPDATE counter SET value = value + ? WHERE id = 1;", [
num,
]);
const rows = await req.conn.query("SELECT value FROM counter WHERE id = 1;");
res.json({ counter: rows[0]?.value });
});
app.use((err, req, res, next) => {
console.error(err);
res.status(500).json({ error: "internal" });
});
const server = app.listen(process.env.PORT || 3000, () => {
console.log('Listening on port ' + server.address().port);
});
function shutDown() {
server.close(() => {
process.exit(0);
});
}
process.on('SIGTERM', shutDown);
process.on('SIGINT', shutDown);