-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresponse.js
More file actions
90 lines (84 loc) · 3.38 KB
/
response.js
File metadata and controls
90 lines (84 loc) · 3.38 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
let fs = require('fs'),
ejs = require('ejs'),
config = require('./config'),
votingPagePath = __dirname + "/web/html/Voting.ejs",
votingCreatePagePath = __dirname + "/web/html/QuestionCreate.ejs",
dashBoardPagePath = __dirname + "/web/html/DashBoard.ejs",
loginPagePath = __dirname + "/web/html/Login.ejs",
questionListPagePath = __dirname + "/web/html/QuestionList.ejs",
adminQuestionListPagePath = __dirname + "/web/html/AdminQuestionList.ejs",
questionEditPagePath = __dirname + "/web/html/QuestionEdit.ejs",
votingPage = fs.readFileSync(votingPagePath, 'utf8'),
votingCreatePage = fs.readFileSync(votingCreatePagePath, 'utf8'),
dashBoardPage = fs.readFileSync(dashBoardPagePath, 'utf8'),
loginPage = fs.readFileSync(loginPagePath, 'utf8'),
questionListPage = fs.readFileSync(questionListPagePath, 'utf8'),
adminQuestionListPage = fs.readFileSync(adminQuestionListPagePath, 'utf8'),
questionEditPage = fs.readFileSync(questionEditPagePath, 'utf8');
let Response = function () {};
Response.prototype = {
getVotingPage : function (res, data) {
res.writeHead(200, {"Content-Type": "text/html"});
res.end(ejs.render(votingPage, data));
},
getVotingCreatePage : function (res) {
res.writeHead(200, {"Content-Type": "text/html"});
res.end(ejs.render(votingCreatePage, config.color));
},
getDashBoardPage : function (res, question, isadmin=false) {
res.writeHead(200, {"Content-Type": "text/html"});
res.end(ejs.render(dashBoardPage, {
id: question.id,
q: question.q,
image: question.image,
a: question.a,
isadmin: isadmin
}));
},
getLoginPage : function (res) {
res.writeHead(200, {"Content-Type": "text/html"});
res.end(loginPage);
},
getQuestionListPage : function (res, qList) {
res.writeHead(200, {"Content-Type": "text/html"});
res.end(ejs.render(questionListPage, {qList: qList}));
},
getAdminQuestionListPage : function (res, qList) {
res.writeHead(200, {"Content-Type": "text/html"});
res.end(ejs.render(adminQuestionListPage, {qList: qList}));
},
getQuestionEditPage : function (res, question) {
let obj = Object.assign({},config.color, question);
res.writeHead(200, {"Content-Type": "text/html"});
res.end(ejs.render(questionEditPage, obj));
},
getSuccess: function(res){
res.writeHead(200, {"Content-Type": "text/html"});
res.end("success!");
},
getCreated: function(res, id){
res.writeHead(200, {"Content-Type": "text/html"});
res.end(JSON.stringify(id));
},
getRatio: function(res, data){
res.writeHead(200, {"Content-Type": "application/json"});
res.end(JSON.stringify(data));
},
getPageNotFound: function (res) {
res.writeHead(404, {"Content-Type": "text/html"});
res.end("page not found!");
},
getBadRequest: function(res){
res.writeHead(400, {"Content-Type": "text/html"});
res.end("bad request!");
},
getPermissionDenied: function(res){
res.writeHead(403, {"Content-Type": "text/html"});
res.end("permission denied!");
},
getConflict: function(res){
res.writeHead(409, {"Content-Type": "text/html"});
res.end("conflict!");
},
};
exports.response = new Response();