-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.js
49 lines (37 loc) · 1.04 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
const express = require("express");
const fetch = require("node-fetch");
const fs = require("fs");
const path = require("path");
const morgan = require("morgan");
let config;
if (fs.existsSync("./config.json")) {
config = require("./config.json");
} else {
console.log(`Please rename the config.sample.json file to config.json`);
process.exit(0);
}
let mood = 0;
try {
mood = require("./mood.json");
} catch {}
const app = express();
app.use(morgan("common"));
app.use(express.static(path.join(__dirname, "public")));
app.get("/mood", (req, res) => {
return res.json(mood);
});
app.post("/mood/:value", (req, res) => {
if (isNaN(req.params.value)) return res.sendStatus(400);
mood = req.params.value;
fs.writeFileSync(path.join(__dirname, "mood.json"), mood);
fetch(config.WEBHOOK).then(() => {
console.log("Notification sent!");
});
return res.sendStatus(200);
});
app.get("/config", (req, res) => {
return res.json(config.CONFIG);
});
app.listen(config.PORT, () => {
console.log(`app listening on port ${config.PORT}`);
});