-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
104 lines (90 loc) · 2.71 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
const express = require("express");
const bodyParser = require("body-parser");
const session = require("express-session");
const PostgresSurveyStorage = require("./db-adapters/postgres");
const apiBaseAddress = "/api";
const app = express();
app.use(
session({
secret: "mysecret",
resave: true,
saveUninitialized: true,
// cookie: { secure: true }
})
);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
function getStorage (req) {
const storage = new PostgresSurveyStorage(req.session);
return storage;
}
function sendJsonResult (res, obj) {
res.setHeader("Content-Type", "application/json");
res.send(JSON.stringify(obj));
}
app.get(apiBaseAddress + "/getActive", (req, res) => {
const storage = getStorage(req);
storage.getSurveys((result) => {
sendJsonResult(res, result);
});
});
app.get(apiBaseAddress + "/getSurvey", (req, res) => {
const storage = getStorage(req);
const surveyId = req.query["surveyId"];
storage.getSurvey(surveyId, (result) => {
sendJsonResult(res, result);
});
});
app.get(apiBaseAddress + "/changeName", (req, res) => {
const storage = getStorage(req);
const id = req.query["id"];
const name = req.query["name"];
storage.changeName(id, name, (result) => {
sendJsonResult(res, result);
});
});
app.get(apiBaseAddress + "/create", (req, res) => {
const storage = getStorage(req);
const name = req.query["name"];
storage.addSurvey(name, (survey) => {
sendJsonResult(res, survey);
});
});
app.post(apiBaseAddress + "/changeJson", (req, res) => {
const storage = getStorage(req);
const id = req.body.id;
const json = req.body.json;
storage.storeSurvey(id, null, json, (survey) => {
sendJsonResult(res, survey);
});
});
app.post(apiBaseAddress + "/post", (req, res) => {
const storage = getStorage(req);
const postId = req.body.postId;
const surveyResult = req.body.surveyResult;
storage.postResults(postId, surveyResult, (result) => {
sendJsonResult(res, result.json);
});
});
app.get(apiBaseAddress + "/delete", (req, res) => {
const storage = getStorage(req);
const id = req.query["id"];
storage.deleteSurvey(id, () => {
sendJsonResult(res, { id: id });
});
});
app.get(apiBaseAddress + "/results", (req, res) => {
const storage = getStorage(req);
const postId = req.query["postId"];
storage.getResults(postId, (result) => {
sendJsonResult(res, result);
});
});
app.get(["/", "/about", "/run/*", "/edit/*", "/results/*"], (_, res) => {
res.sendFile("index.html", { root: __dirname + "/../public" });
});
app.use(express.static(__dirname + "/../public"));
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log("Listening on port: " + port + "...");
});