-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
337 lines (297 loc) · 11.2 KB
/
app.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Node.JS Packages
var express = require("express");
var bodyParser = require("body-parser");
var fs = require("fs");
var colors = require("colors");
var cookieParser = require('cookie-parser');
var Cookies = require("cookies");
colors.setTheme({
title: ['white', 'italic'],
error: ['bgRed', 'white', 'title'],
info: ['bgYellow', 'white', 'italic'],
success: ['bgGreen', 'white'],
});
// Imports for External Refresh Node.JS Functions
var list = require('./modules/list.js');
var examBoard = require('./modules/exam.js');
var scrape = require('./modules/scrape.js');
var convert = require('./modules/convert.js');
var research = require('./modules/research.js');
var question = require('./modules/question.js');
var answer = require('./modules/answer.js');
var database = require('./modules/database.js');
//console.log("App.JS: ".title + " Successfully Imported External Functions".success);
// Instantiating External Refresh Packages
var listModule = new list();
var examBoardModule = new examBoard();
var scrapeModule = new scrape();
var convertModule = new convert();
var researchModule = new research();
var questionModule = new question();
var answerModule = new answer();
var databaseModule = new database();
//console.log("App.JS: ".bold + " Successfully Instantiated Packages".blue);
var databaseModule = new database();
// Creating Express.JS Web Server
var app = express();
//console.log("App.JS: ".title + "Initialised Web Server".success);
// Initialising Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
//console.log("App.JS: ".title + " Successfully Initialised Middleware".success);
// Creating Static Public Folder
app.use(express.static('public'));
//console.log("App.JS: ".title + " Successfully Created Public Folder".success);
// Pages Cache
var homeTemplate = "";
var dashboardTemplate = "";
var learnTemplate = "";
function refreshTemplateCache(callback) {
count = 0;
// Homepage Template
fs.readFile("pages/index.html", "utf-8", function(err, data) {
homeTemplate = data;
count++;
if ((count == 3) && (callback !== undefined)) {
callback();
}
});
// Dashboard Template
fs.readFile("pages/dashboard.html", "utf-8", function(err, data) {
dashboardTemplate = data;
count++;
if ((count == 3) && (callback !== undefined)) {
callback();
}
});
// Learning Environment Template
fs.readFile("pages/learn.html", "utf-8", function(err, data) {
learnTemplate = data;
count++;
if ((count == 3) && (callback !== undefined)) {
callback();
}
});
}
// Template Engine
function refreshRenderer(template, elementsToReplace, callback) {
// Variables //
var bracket = false;
var handlebar = false;
var endBracket = false;
var tagID = "";
var start = 0;
var end = 0;
var toReplace = [];
// Collect Points to Template //
for (i in template) {
if (handlebar == true) {
if (template[i] == "}") {
if (endBracket == true) {
handlebar = false;
end = i;
toReplace.push([tagID.trim(), start, end]);
tagID = "";
}
endBracket = true;
} else {
tagID += template[i];
}
} else {
endBracket = false;
}
if (template[i] == "{") {
if (bracket == true) {
handlebar = true;
} else {
start = i;
}
bracket = true;
} else {
bracket = false;
}
}
var renderedWebpage = "";
var previousStartPoint = 0;
for (e in toReplace) {
renderedWebpage += template.substring(previousStartPoint, parseInt(toReplace[e][1]));
renderedWebpage += elementsToReplace[toReplace[e][0]];
previousStartPoint = parseInt(toReplace[e][2]) + 1;
}
renderedWebpage += template.substring(previousStartPoint, template.length);
callback(renderedWebpage);
}
// App Routes
app.get("/", function(req, res) {
fs.readFile("pages/index.html", "ASCII", function(err, data) {
res.send(data);
});
});
app.post("/register", function(req, res) {
var firstName = req.body.fName;
var lastName = req.body.lName;
var email = req.body.eMail;
var password = req.body.pass;
var username = req.body.uName;
databaseModule.addUser(firstName, lastName, email, password, username, function(output) {
if (output == true) {
fs.readFile("pages/syllabus-choice.html", "ASCII", function(err, data) {
res.send(data);
});
} else {
fs.readFile("pages/syllabus-choice.html", "ASCII", function(err, data) {
res.send(data);
});
}
});
});
app.get("/learn", function(req, res) {
fs.readFile("pages/syllabus-choice.html", "ASCII", function(err, data) {
res.send(data);
});
});
app.get("/dashboard", function(req, res) {
console.log("GET /dashboard");
fs.readFile("pages/dashboard.html", "ASCII", function(err, data) {
res.send(data);
});
});
app.get("/login", function(req, res) {
fs.readFile("pages/login.html", "ASCII", function(err, data) {
res.send(data);
});
});
app.get("/logout", function(req, res) {
res.clearCookie('emai');
res.clearCookie('loggedin');
res.redirect(301, "/");
});
app.post("/checkLogin", function(req, res) {
var cookies = new Cookies(req, res),
unsigned, signed, tampered;
var password = req.body.password;
var email = req.body.email;
databaseModule.login(email, password, function(output) {
if (output != false) {
console.log(output.Email, output.FirstName, output.LastName);
cookies.set("emai", output.Email, {
httpOnly: false
});
res.cookie('emai', output.Email, {
maxAge: 900000,
httpOnly: true
});
cookies.set("forename", output.FirstName, {
httpOnly: false
});
cookies.set("surname", output.LastName, {
httpOnly: false
});
cookies.set("loggedin", true, {
httpOnly: false
});
res.redirect(301, '/dashboard?s=loggedin&email=' + email);
} else {
res.redirect(301, '/dashboard?s=incorrect_username_password');
}
});
});
app.post("/CIE", function(req, res) {
console.log("App.JS".title + ": " + "Looking for subjects".success);
listModule.examBoardCIE(function(data) {
res.send({
subjectData: data
});
});
});
app.post("/CIEsubject", function(req, res) {
console.log("App.JS".title + ": " + "Looking for syllabii".success);
var syllabusNumber = String(req.body.syllabusNumber);
examBoardModule.collectURLs(syllabusNumber, function(data) {
res.send(data);
});
});
app.post("/getQuestions", function(req, res) {
console.log("App.JS".title + ": " + "Looking for questions".success);
try {
var email = req.body.email;
var password = req.body.password;
var examBoard = req.body.examBoard;
var subject = req.body.subject;
var syllabus = req.body.syllabus;
var url = req.body.url;
databaseModule.login(email, password, function(output) {
if (output != false) {
databaseModule.createSyllabusEntry(email, examBoard, subject, syllabus, function() {
scrapeModule.convertPDF(examBoard, subject, syllabus, url, function() {
console.log("App.JS".title + ": " + "Converted PDF".success);
scrapeModule.scrape(examBoard, subject, syllabus, function(points) {
console.log("App.JS".title + ": " + "Scraped".success);
convertModule.convert(points, function(searchFields) {
console.log("App.JS".title + ": " + "Converted".success);
researchModule.researchTopic(searchFields, function(usefulSentences) {
console.log("App.JS".title + ": " + "Researched".success);
questionModule.question(usefulSentences, function(toStore) {
//console.log(toStore);
toStore = JSON.parse(toStore);
if (!toStore) {
res.send(toStore)
} else {
// we have some duplicate keywords in the data so we need to remove them
var newToStore = [[]];
var keywords = [];
for (var i = 0; i < toStore[0].length - 1; i++) {
//console.log(toStore[0][i]);
if (keywords.indexOf(toStore[0][i][0]) == -1) {
keywords.push(toStore[0][i][0]);
newToStore[0].push(toStore[0][i]);
}
else {
}
}
newToStore.push(toStore[1]);
fs.writeFile("files/" + subject + ".sentenceData", newToStore, function(err) {
//if (err) throw err;
res.send(newToStore);
console.log(newToStore);
console.log("App.JS".title + ": " + "Sent data to client".success);
});
}
});
})
});
});
});
});
} else {
}
});
} catch (err) {
console.log(String(err).red);
}
});
app.post("/getLearnData", function(req, res) {
var subject = req.body.examSubject;
subject = "0620";
fs.readFile("files/" + subject + ".sentenceData", function(err, data) {
if (err) {
res.send("Error :(");
} else {
var buffer = JSON.parse(data);
console.log(buffer);
res.send(buffer);
}
});
});
// Initialising the Express.JS Web Server to Listen on Port process.argv[2]
if (process.argv[2] == undefined) {
process.argv[2] = 80;
}
var server = app.listen(process.argv[2], function() {
var host = server.address().address;
var port = server.address().port;
refreshTemplateCache();
console.log("App.JS".title + ": " + "Refresh Running at".success + " " + String(host).blue + ":" + String(port).blue);
});