-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
87 lines (75 loc) · 2.19 KB
/
app.js
File metadata and controls
87 lines (75 loc) · 2.19 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
// SERVER SIDE
const express = require("express");
const path = require("path");
let app = express();
let publicPath = path.resolve(__dirname, "public");
app.use(express.static(publicPath));
app.listen(3000, () => console.log("Starting up ..."));
console.log("In server side");
class Course {
constructor(
title,
description,
credits,
designation,
completed,
used,
grade,
level,
prereqs,
coreqs,
offered
)
{
this.title = title;
this.description = description;
this.credits = credits;
this.designation = designation;
this.completed = completed;
this.used = used;
this.grade = grade;
this.level = level;
this.prereqs = prereqs;
this.coreqs = coreqs;
this.offered = offered;
}
}
let courses = []; // array of courses
let counter = 0; // counter for courses array
const fs = require('fs');
const bus_courses = fs.readFileSync('data/bus_courses.json', 'utf8');
const cse_courses = fs.readFileSync('data/cse_courses.json', 'utf8');
const math_courses = fs.readFileSync('data/math_courses.json', 'utf8');
// convert text to JSON
const bus_courses_json = JSON.parse(bus_courses);
const cse_courses_json = JSON.parse(cse_courses);
const math_courses_json = JSON.parse(math_courses);
// read json data into courses array
readData(bus_courses_json);
readData(cse_courses_json);
readData(math_courses_json);
// function to read json data into courses array
function readData(filename) {
// console.log("In readData function");
for (let i = 0; i < filename.length; i++) {
courses[counter] = new Course(
filename[i].title,
filename[i].description,
filename[i].credits,
filename[i].designation,
filename[i].completed,
filename[i].used,
filename[i].grade,
filename[i].level,
filename[i].prereqs,
filename[i].coreqs,
filename[i].offered
);
counter++;
}
}
console.log(courses);
// handle GET request for courses
app.get("/courses", (req, res) => {
res.send(JSON.stringify(courses));
});