-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
104 lines (88 loc) · 3.16 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
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
// Dependencies
// =============================================================
const express = require("express");
const path = require("path");
const app = require("./lib/app");
// Sets up the Express App
// =============================================================
const server = express();
const PORT = process.env.PORT || 3000;
// Array to hold all created employees
const employees = [
];
// Sets up the Express app to handle data parsing
server.use(express.urlencoded({ extended: true }));
server.use(express.json());
// Declare folder to be able to access from files
server.use('/Assets', express.static(__dirname + "/Assets"));
// Get path for the index page
server.get("/", (req,res) =>{
res.sendFile(path.join(__dirname, "index.html"))
});
// Get path for the team page
server.get("/pages/team", (req,res) =>{
res.sendFile(path.join(__dirname, "/pages/team.html"));
});
// get path to return the employees json obj
server.get("/api/employees", function(req, res) {
return res.json(employees);
});
// get path to return specific employee json obj
server.get("/api/employees/:employeeId", (req, res)=>{
const employeeId = req.params.employeeId;
for(const currentEmployee of employees)
if(employeeId === currentEmployee.id)
return res.json(currentEmployee);
return res.json(false);
});
// post path to create the teamfile
server.post("/create/teamFile", (req,res) =>{
app.createFile(employees);
});
// post path the create a new employee and push into the employee array
server.post("/api/employees", function(req, res) {
const newEmployee = req.body;
employees.push(app.createGUIEmployee(newEmployee));
});
// Post path to update the employee information
server.post("/api/update/employee", function(req, res) {
const employeeInfo = req.body;
for(const currentEmployee of employees)
if(employeeInfo.id === currentEmployee.id)
{
currentEmployee.email = employeeInfo.email;
switch(currentEmployee.role)
{
case "Manager":
currentEmployee.officeNumber = employeeInfo.extraInfo;
break;
case "Intern":
currentEmployee.school = employeeInfo.extraInfo;
break;
case "Engineer":
currentEmployee.github = employeeInfo.extraInfo;
break;
}
}
});
// Post path to delete a employee object
server.post("/api/delete/employee", function(req, res) {
const employeeInfo = req.body;
for(let i = 0; i < employees.length; i++)
{
console.log(employeeInfo.id);
console.log(employees[i]);
console.log(employeeInfo.id == employees[i].id);
if(employeeInfo.id == employees[i].id)
employees.splice(i,1);
}
});
// get path to return error page if user tries to traverse to
server.get("*", function(req,res){
res.sendFile(path.join(__dirname, "/pages/page-not-found.html"));
});
// Starts the server to begin listening
// =============================================================
server.listen(PORT, function() {
console.log("App listening on PORT " + PORT);
});