-
Notifications
You must be signed in to change notification settings - Fork 93
/
index.js
103 lines (89 loc) · 2.65 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
var express = require("express");
var bodyParser = require("body-parser");
var session = require("express-session");
var inmemorydbadapter = require("./inmemorydbadapter");
var apiBaseAddress = "/api";
var 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 getDBAdapter(req) {
var db = new inmemorydbadapter(req.session);
return db;
}
function sendJsonResult(res, obj) {
res.setHeader("Content-Type", "application/json");
res.send(JSON.stringify(obj));
}
app.get(apiBaseAddress + "/getActive", function (req, res) {
var db = getDBAdapter(req);
db.getSurveys(function (result) {
sendJsonResult(res, result);
});
});
app.get(apiBaseAddress + "/getSurvey", function (req, res) {
var db = getDBAdapter(req);
var surveyId = req.query["surveyId"];
db.getSurvey(surveyId, function (result) {
sendJsonResult(res, result);
});
});
app.get(apiBaseAddress + "/changeName", function (req, res) {
var db = getDBAdapter(req);
var id = req.query["id"];
var name = req.query["name"];
db.changeName(id, name, function (result) {
sendJsonResult(res, result);
});
});
app.get(apiBaseAddress + "/create", function (req, res) {
var db = getDBAdapter(req);
var name = req.query["name"];
db.addSurvey(name, function (survey) {
sendJsonResult(res, survey);
});
});
app.post(apiBaseAddress + "/changeJson", function (req, res) {
var db = getDBAdapter(req);
var id = req.body.id;
var json = req.body.json;
db.storeSurvey(id, null, json, function (survey) {
sendJsonResult(res, survey);
});
});
app.post(apiBaseAddress + "/post", function (req, res) {
var db = getDBAdapter(req);
var postId = req.body.postId;
var surveyResult = req.body.surveyResult;
db.postResults(postId, surveyResult, function (result) {
sendJsonResult(res, result.json);
});
});
app.get(apiBaseAddress + "/delete", function (req, res) {
var db = getDBAdapter(req);
var id = req.query["id"];
db.deleteSurvey(id, function (result) {
sendJsonResult(res, { id: id });
});
});
app.get(apiBaseAddress + "/results", function (req, res) {
var db = getDBAdapter(req);
var postId = req.query["postId"];
db.getResults(postId, function (result) {
sendJsonResult(res, result);
});
});
app.get(["/", "/about", "/run/*", "/edit/*", "/results/*"], function (req, res, next) {
res.sendFile("index.html", { root: __dirname + "/public" });
});
app.use(express.static(__dirname + "/public"));
app.listen(process.env.PORT || 3000, function () {
console.log("Listening!");
});