-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
386 lines (328 loc) · 12.4 KB
/
server.js
File metadata and controls
386 lines (328 loc) · 12.4 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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//Required dependencies
var http = require("http");
var fs = require("fs");
var url = require("url");
var async = require("async");
var moment = require("moment");
//JSON objects to be read from files
var studJSON = [];
var sub_1 = [];
var sub_2 = [];
var sub_3 = [];
var sub_4 = [];
//ID module
var idModule = require("./IdModule/module.js");
const PORT = 9090;
//Object to read request JSON value
var reqJSON = '';
var resJSON = [];
var start;
//Created simple http server
var server = http.createServer(handleRequest);
//Handler function to handle http request
function handleRequest(req, res) {
//Moment plug-in to log particular moment time between two steps
//start = moment();
//Capture request queryString and path name
var query = url.parse(req.url, true).query;
var pathName = url.parse(req.url, true).pathname;
//'PUT' method to save new student record
if(req.method == 'PUT' && pathName == '/api/student') {
console.log("[INFO] Method: 'PUT' path: /api/student");
//Read the JSON from request
readReqJSON(req);
//Read JSON files from local storage
readFiles();
//Wait for some time to read JSON object from request
setTimeout(function() {
//start = moment();
//console.log("[INFO] After read JSON: ", moment().diff(start, "seconds", true));
var jsonval = JSON.parse(reqJSON);
var qEmail = jsonval.email;
//Call IdModule to get id from JSON for requested email
//Check if the student with the 'email' is already present
var qId = idModule.getId(qEmail, studJSON.students);
//Return if the student with email already exist
if(qId != null) {
console.log("[INFO] Student already present");
res.writeHead(500);
res.end('Student record already present');
return;
}
//start = moment();
//console.log("[INFO] IdModule complete: ", moment().diff(start, "seconds", true));
//Generate new id for student
var newId = Math.floor(Math.random()*1000);
var name = jsonval.name;
//console.log('[INFO] Id: ' + newId + ' Email: ' + qEmail + ' Name: ' + name);
//Append student record to student JSON
studJSON.students.push({id: newId, email: qEmail, name: name});
//Save student enrolment data to particular subjects
var newEnrollment = jsonval.enrolledSubjects;
var resEnrollment = [];
async.forEach(Object.keys(newEnrollment), function(key, next) {
if(sub_1.subjectId == newEnrollment[key].subjectId) {
sub_1.enrolledStudents.push({id: newId, score: newEnrollment[key].score});
resEnrollment.push({subjectId: sub_1.subjectId, subjectName: sub_1.subjectName});
}
if(sub_2.subjectId == newEnrollment[key].subjectId) {
sub_2.enrolledStudents.push({id: newId, score: newEnrollment[key].score});
resEnrollment.push({subjectId: sub_2.subjectId, subjectName: sub_2.subjectName});
}
if(sub_3.subjectId == newEnrollment[key].subjectId) {
sub_3.enrolledStudents.push({id: newId, score: newEnrollment[key].score});
resEnrollment.push({subjectId: sub_3.subjectId, subjectName: sub_3.subjectName});
}
if(sub_4.subjectId == newEnrollment[key].subjectId) {
sub_4.enrolledStudents.push({id: newId, score: newEnrollment[key].score});
resEnrollment.push({subjectId: sub_4.subjectId, subjectName: sub_4.subjectName});
}
});
resJSON = [];
//Generate response JSON
resJSON.push({id: newId, email: qEmail, name: name, enrolledSubjects: resEnrollment});
console.log(JSON.stringify(resJSON));
//Write the modified JSON files back to local file storage
writeFiles();
res.writeHead(200, "{content-type: application/json}");
res.end(JSON.stringify(resJSON));
return;
//start = moment();
//console.log("[INFO] Iterating through enroll complete: ", moment().diff(start, "seconds", true));
}, 1000);
console.log("[INFO] Method: 'PUT' path: /api/student Complete");
//'GET' request to return student data for given student id if present
} else if(req.method == 'GET' && pathName.indexOf('/api/student') != -1) {
//Read file from local file storage
readFiles();
//Student id from request
var retId = pathName.split('/')[3];
console.log("[INFO] Method: 'GET' path: /api/student/{id}");
//Find student with id
var student = studJSON.students.filter(function(value) {
if(retId == value.id) {
return value;
}
});
var enrollData = [];
//If student record found in student JSON then get enrolment record for the same student for all subjects
//he/she is registered
if(student[0] != null) {
for(var enroll in sub_1.enrolledStudents) {
if(sub_1.enrolledStudents[enroll].id == retId) {
enrollData.push({subjectId: sub_1.subjectId, subjectName: sub_1.subjectName});
}
}
for(var enroll in sub_2.enrolledStudents) {
if(sub_2.enrolledStudents[enroll].id == retId) {
enrollData.push({subjectId: sub_2.subjectId, subjectName: sub_2.subjectName});
}
}
for(var enroll in sub_3.enrolledStudents) {
if(sub_3.enrolledStudents[enroll].id == retId) {
enrollData.push({subjectId: sub_3.subjectId, subjectName: sub_3.subjectName});
}
}
for(var enroll in sub_4.enrolledStudents) {
if(sub_4.enrolledStudents[enroll].id == retId) {
enrollData.push({subjectId: sub_4.subjectId, subjectName: sub_4.subjectName});
}
}
//Prepare response JSON with student data
var result = [{id: retId, email: student[0].email, name: student[0].name, enrollmentSubjects: enrollData}];
res.writeHead(200, "{content-type: application/json}");
res.end(JSON.stringify(result));
console.log("[INFO] Get request completed");
return;
} else {
res.writeHead(404);
res.end("Student not found");
return;
}
console.log("[INFO] Method: 'GET' path: /api/student/{id} Complete");
//'GET' request to return all student records
} else if(req.method == 'GET' && pathName == '/api/students') {
console.log("[INFO] Method: 'GET' path: /api/students");
//Process request
console.log("[INFO] Method: 'GET' path: /api/students Complete");
//'POST' request to update a particular student record
} else if(req.method == 'POST' && pathName.indexOf('/api/student') != -1) {
console.log("[INFO] Method: 'POST' path: /api/student/{id}");
var updateId = pathName.split('/')[3];
//Read JSON from request
readReqJSON(req);
setTimeout(function() {
var jsonval = JSON.parse(reqJSON);
//Update student record if the student found in Student JSON
for(var stud in studJSON.students) {
if(studJSON.students[stud].id == updateId) {
studJSON.students[stud].email = jsonval.email;
studJSON.students[stud].name = jsonval.name;
}
}
//Update student's enrolment records for all subjects
for(var inEnroll in jsonval.enrolledSubjects) {
var removeIndex = -1;
if(jsonval.enrolledSubjects[inEnroll] == sub_1.subjectId) {
for(var index in sub_1.enrolledStudents) {
if(sub_1.enrolledStudents[index].id == jsonval.enrolledSubjects[inEnroll].id) {
removeIndex = index;
}
}
}
if(removeIndex != -1) {
sub_1.enrolledStudents.splice(removeIndex, 1);
removeIndex = -1;
}
if(jsonval.enrolledSubjects[inEnroll] == sub_2.subjectId) {
for(var index in sub_2.enrolledStudents) {
if(sub_2.enrolledStudents[index].id == jsonval.enrolledSubjects[inEnroll].id) {
removeIndex = index;
}
}
}
if(removeIndex != -1) {
sub_2.enrolledStudents.splice(removeIndex, 1);
removeIndex = -1;
}
if(jsonval.enrolledSubjects[inEnroll] == sub_3.subjectId) {
for(var index in sub_3.enrolledStudents) {
if(sub_3.enrolledStudents[index].id == jsonval.enrolledSubjects[inEnroll].id) {
removeIndex = index;
}
}
}
if(removeIndex != -1) {
sub_3.enrolledStudents.splice(removeIndex, 1);
removeIndex = -1;
}
if(jsonval.enrolledSubjects[inEnroll] == sub_4.subjectId) {
for(var index in sub_4.enrolledStudents) {
if(sub_4.enrolledStudents[index].id == jsonval.enrolledSubjects[inEnroll].id) {
removeIndex = index;
}
}
}
if(removeIndex != -1) {
sub_4.enrolledStudents.splice(removeIndex, 1);
removeIndex = -1;
}
}
}, 1000);
console.log("[INFO] Method: 'POST' path: /api/student/{id} Complete");
//'DELETE' request to delete particular student record along with its enrolment in any subject
} else if(req.method == 'DELETE' && pathName.indexOf('/api/student') != -1) {
console.log("[INFO] Method: 'DELETE' path: /api/student/{id}");
var delId = pathName.split('/')[3];
//Read Student and subjects files from local storage
readFiles();
setTimeout(function() {
var jsonval = studJSON;
var index = -1;
//Delete record from student JSON if student found with given id
for(var stud in jsonval.students) {
if(jsonval.students[stud].id == delId) {
index = stud;
//delete jsonval.students[stud];
}
}
jsonval.students.splice(index, 1);
//If student record is not present in Student JSON itself then student is not present discard the request
//and return proper message
if(index == -1) {
res.writeHead(404);
res.end("No student found !!!");
return;
}
//jsonval.students.splice(index, 1);
//Continue if student found in Student JSON
for(var stud in sub_1.enrolledStudents) {
if(sub_1.enrolledStudents[stud].id == delId) {
index = stud;
//delete sub_1.enrolledStudents[stud];
}
}
sub_1.enrolledStudents.splice(index, 1);
for(var stud in sub_2.enrolledStudents) {
if(sub_2.enrolledStudents[stud].id == delId) {
index = stud;
//delete sub_2.enrolledStudents[stud];
}
}
sub_2.enrolledStudents.splice(index, 1);
for(var stud in sub_3.enrolledStudents) {
if(sub_3.enrolledStudents[stud].id == delId) {
index = stud;
//delete sub_3.enrolledStudents[stud];
}
}
sub_3.enrolledStudents.splice(index, 1);
for(var stud in sub_4.enrolledStudents) {
if(sub_4.enrolledStudents[stud].id == delId) {
index = stud;
//delete sub_4.enrolledStudents[stud];
}
}
sub_4.enrolledStudents.splice(index, 1);
var result = {id: delId};
//Write updated Students JSON files back to local storage
writeFiles();
res.writeHead(200, "{content-type: application/json}");
res.end(JSON.stringify(result));
}, 1000);
console.log("[INFO] Method: 'DELETE' path: /api/student/{id}");
} else {
//Discard request if it does not match with the provided request method and request URL
res.writeHead(400);
res.end();
}
}
//Function to read JSON from request
var readReqJSON = function(req) {
//start = moment();
//console.log("[INFO] Read JSON: ", moment().diff(start, "seconds", true));
reqJSON = '';
console.log('[INFO] Reading JSON object from request');
req.on('data', function(data) {
reqJSON += data;
});
req.on('end', function() {
//start = moment();
//console.log("[INFO] Complete read JSON: ", moment().diff(start, "seconds", true));
console.log('[INFO] Reading JSON object from request completed');
//console.log(JSON.parse(reqJSON));
});
}
//Function to read JSON files from local storage
function readFiles() {
//console.log("readFiles()");
studJSON = readJSON('./Source/student.json');
sub_1 = readJSON('./Source/sub_1.json');
sub_2 = readJSON('./Source/sub_2.json');
sub_3 = readJSON('./Source/sub_3.json');
sub_4 = readJSON('./Source/sub_4.json');
}
//Function to write JSON files to local storage
function writeFiles() {
writeJSON('./Source/student.json', studJSON);
writeJSON('./Source/sub_1.json', sub_1);
writeJSON('./Source/sub_2.json', sub_2);
writeJSON('./Source/sub_3.json', sub_3);
writeJSON('./Source/sub_4.json', sub_4);
}
var readJSON = function(path) {
//console.log("readJSON: " + path);
var file = fs.readFileSync(path);
//console.log("file: " + file);
return JSON.parse(file);
}
var writeJSON = function(path, data) {
fs.writeFile(path, JSON.stringify(data), function (err) {
if (err) throw err;
//console.log('It\'s saved!');
});
}
//Start server on specified PORT variable
server.listen(PORT, function(err) {
console.log("[INFO] Server started on " + PORT);
});