-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
223 lines (201 loc) · 6.05 KB
/
Copy pathserver.js
File metadata and controls
223 lines (201 loc) · 6.05 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Настройки
const setup = {port:8080}
// Подключаем зависимости
import express from 'express';
import bodyParser from 'body-parser';
import path from 'path';
import { fileURLToPath } from 'url';
import Quest from "./quests/quest.js";
import Question from "./questions/question.js";
import mongoose from 'mongoose';
import cookieParser from 'cookie-parser';
// формируем пересенные
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
let quest = new Quest();
let question = new Question();
const app = express ();
app.set('view engine', 'ejs');
app.use('views',express.static(__dirname + ('views')));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(cookieParser('secret key'))
/*
* database connection
* */
// const mongo_url = 'mongodb://localhost:27017/quest';
const mongo_url = 'mongodb://questionnaire-200:pQJiNmjthbVCnTx7xsP_@77c7cbc9-5796-41a7-9e0b-00cc1fdef3ad.questionnaire-200.mongo.a.osc-fr1.scalingo-dbs.com:39447/questionnaire-200?replicaSet=questionnaire-200-rs0&ssl=true';
// connect to local DB
mongoose.connect(mongo_url).catch(error => handleError(error));
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error: "));
db.once("open", function () {
console.log("Connected successfully");
});
/**
* Integrate (css and js) to structure
*/
app.use('/js', express.static( __dirname + '/js') );
app.use('/img', express.static( __dirname + '/img') );
app.use('/public', express.static(__dirname + '/css') );
app.use('/public', express.static(__dirname + '/node_modules/bootstrap/dist') );
app.use('/public', express.static(__dirname + '/node_modules/font-awesome/') );
app.use('/public', express.static(__dirname + '/node_modules/popper.js/dist') );
app.use('/js', express.static(__dirname + '/node_modules/jquery/dist'));
app.use('/js', express.static(__dirname + '/node_modules/@poperjs/core/dist'));
app.use('/js/quests/', express.static(__dirname + '/quests/'));
app.use('/js/questions/', express.static(__dirname + '/questions/'));
app.use('/public/js/quiz/', express.static(__dirname + '/quiz/'));
/**
* Routes
*/
app.get('/', async (req, res) => {
let quests = await quest.getList();
res.render('index',{
title:'Опросы на сайте',
quests: quests
})
});
app.get('/login', async (req, res) => {
res.render('auth/login')
});
app.post('/login', async (req, res) => {
let data = req.body
if(data.login === 'Admin' && data.password === 'TopSecret'){
res.cookie('auth', 'true', {
maxAge: 2*60*60*1000,
httpOnly:true
});
res.redirect('/quests');
}
});
app.get('/quiz/:questId', async (req, res) => {
let paramId = req.params.questId;
const result = await quest.getById(paramId);
try {
// res.send(quests);
res.render('public/', {
quest:result
})
} catch (error) {
res.status(500).send(error);
}
});
/**
* Quest routes
*/
app.get('/quests', async (req, res) => {
if(!req.cookies.auth || req.cookies.auth !== 'true'){
res.render('auth/login');
}
let quests1 = await quest.getList();
try {
// res.send(quests);
res.render('quest/index', {
title:'Опросы',
quests:quests1
})
} catch (error) {
res.status(500).send(error);
}
});
app.get('/quests/:questId', async (req, res) => {
let paramId = req.params.questId;
let result = await quest.getById(paramId);
// res.json(result)
try {
// res.send(quests);
res.render('quest/detail', {
itemData:result
})
} catch (error) {
res.status(500).send(error);
}
});
app.delete('/quests/delete/', async (req, res) => {
let id = await (req.body.id);
let result = await quest.deleteQuest(id);
try {
res.json(result);
} catch (error) {
res.status(500).send(error);
}
});
app.post('/quests/create/', async (req, res) => {
let data = await ({
title:req.body.title,
description:req.body.description
})
let result = await quest.addQuest(data);
let response = {
status : 200,
success : 'Added Successfully',
id: result
}
try {
res.json(response);
} catch (error) {
res.status(500).send(error);
}
});
app.post('/quests/edit/', async (req, res) => {
let id = await (req.body.id);
let data = await ({
title:req.body.title,
description:req.body.description
})
let result = await quest.editQuest(id,data);
try {
res.json(result);
} catch (error) {
res.status(500).send(error);
}
});
/**
* Questions routes
*/
app.get('/question', async (req, res) => {
let question = await question.getList();
res.json(question);
});
app.post('/question/create/', async (req, res) => {
let data = await ({
title:req.body.title,
description:req.body.description,
quest:req.body.quest,
answers:req.body.answers.split(',')
})
let result = await question.addQuestion(data);
try {
let response = {
status : 200,
success : 'Added Successfully'
}
res.json(response);
} catch (error) {
res.status(500).send(error);
}
});
app.delete('/question/:id', async (req, res) => {
let id = req.params.id;
let result = await question.delete(id);
try {
let response = {
status : 200,
success : 'Delete Successfully',
data: quest
}
res.json(response);
} catch (error) {
res.status(500).send(error);
}
});
app.get('/question/:questId', async (req, res) => {
let id = req.params.questId;
let result = await question.getListByQuestId(id);
res.json(result);
});
// Слушаем порт и при запуске сервера сообщаем
app.listen(process.env.PORT || setup.port, () => {
console.log('Сервер: порт %s - старт!', setup.port);
});