-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
59 lines (55 loc) · 1.46 KB
/
server.js
File metadata and controls
59 lines (55 loc) · 1.46 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
"use strict";
const express = require('express');
const database = require('./server-lib/database');
main();
function main() {
const app = buildExpressApp();
const server = app.listen(3000, function (err) {
if (err) {
console.error("Unable to start server", err);
process.exitCode = 1;
} else {
console.log("Server listening on http://localhost:3000/")
}
});
process.once('SIGINT', function () {
console.log("CTRL+C received. Shutting down server...");
server.close()
})
}
function buildExpressApp() {
return express()
.use(express.json())
.post("/api/check-password", function (req, res) {
if (req.body.password === "mypass") {
res.json({ok: true})
} else {
res.json({ok: false})
}
})
.get("/api/load-data", function (req, res) {
database.loadDb(function (err, db) {
if (err) {
console.error(err);
res.status(500).send("error");
} else {
// We delay the response so we can experience a "loading screen"
setTimeout(function(){
res.status(200).json(db);
}, 500);
}
});
})
.post("/api/save-data", function (req, res) {
database.saveDb(req.body, function (err) {
if (err) {
console.error(err);
res.status(500).send("error");
} else {
res.status(200).json({ok: true});
}
})
})
.use(express.static("assets"))
;
}