-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
73 lines (64 loc) · 2.28 KB
/
server.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
const express = require("express");
const app = express();
const exphbs = require("express-handlebars");
const fetch = require('node-fetch');
const studentArray = [];
class Student {
constructor(setCity, setCompany, setEmail, setFirstName, setGrades, setId, setLastName, setPic, setSkill) {
this.city = setCity;
this.company = setCompany;
this.email = setEmail;
this.firstName = setFirstName;
this.grades = setGrades;
this.id = setId;
this.lastName = setLastName;
this.pic = setPic;
this.skill = setSkill;
this.average = 0;
}
setAverage() {
let sumGrades = 0;
for (let i = 0; i < this.grades.length; i++) {
sumGrades += parseInt(this.grades[i]);
}
this.average = (sumGrades / this.grades.length);
}
}
app.use(express.static("./views/"));
var HTTP_PORT = process.env.PORT || 8080;
function onHttpStart() {
console.log("Express http server listening on: " + HTTP_PORT);
}
app.engine(".hbs", exphbs({
extname: ".hbs",
defaultLayout: "main",
helpers: {
grades: function(context) {
let out = "<ul>";
let studentIndex = context.data.index
let currentStudent = context.data.root.studentData;
for (let i = 0; i < currentStudent[studentIndex].grades.length; i++) {
out += "<li> Test:" + (i + 1) + " " + parseInt(currentStudent[studentIndex].grades[i]) + "%</li>";
}
out += "</ul>";
return out;
}
}
}));
app.set("view engine", ".hbs");
app.get("/", function(req, res){
fetch("https://api.jsonbin.io/b/5d2382000e09805769fd1117/2")
.then(response => response.json())
.then(json => {
for (let i = 0; i < json.students.length; i++) {
let student = new Student(json.students[i].city, json.students[i].company,
json.students[i].email, json.students[i].firstName, json.students[i].grades,
json.students[i].id, json.students[i].lastName, json.students[i].pic, json.students[i].skill);
student.setAverage();
studentArray.push(student);
}
console.log(studentArray)
res.render("home", { studentData: studentArray});
});
});
app.listen(HTTP_PORT, onHttpStart);