-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
243 lines (167 loc) · 4.93 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
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
// Imports for Node packages
var express = require("express"); // Handles routing
var app = express(); // Server for handling routes, the heart of our app
var axios = require("axios"); // Handles GET, POST etc request and responses
const bodyParser = require("body-parser"); // Middleware for dealing with form input data
// Express server setup (boilerplate code from the docs)
app.set("view engine", "ejs");
// BodyParser middleware setup (boilerplate code from the docs)
app.use(
bodyParser.urlencoded({
extended: true,
})
);
// Tells express where to find any static files like images
app.use(express.static("public"));
/// ** -- ROUTES -- ** ///
// GET Home page which renders the index.ejs template. No data needed for plain HTML.
app.get("/", function (req, res) {
res.render("pages/index");
});
// GET static about page
app.get("/about", function (req, res) {
res.render("pages/about");
});
// POST a new employee route
app.post("/create_employee", function (req, res) {
// Useful for console logging the form inputs
// console.log(console.log(req.body))
// Example of form data for adding a new user
var data =
`{"email":"${req.body.user.email}",
"firstName":"${req.body.user.firstName}",
"id":"${req.body.user.id}",
"lastName":"${req.body.user.lastName}",
"picture":"${req.body.user.picture}",
"title":"${req.body.user.title}"}`;
//DATA is where we input the form data for the new employee we'd like to create.
//REQ.BODY.USER._______ is the location of our data being inputed within the form.
var config = {
method: 'post',
url: 'https://spa-lab-project-ii-default-rtdb.firebaseio.com/data.json',
headers: {
"Content-Type": "text/plain"
},
data : data
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
res.redirect("/directory");
});
// GET Form to add new employee (GET the form first, then the forms "submit" button handles the POST request.
app.get("/create_employee", function (req, res) {
res.render("pages/create_employee");
});
// GET Directory of employees, returns an array of objects from the server.
app.get("/directory", function (req, res) {
var config = {
method: 'get',
url: 'https://spa-lab-project-ii-default-rtdb.firebaseio.com/.json',
headers: { }
};
axios(config)
.then(function (response) {
console.log(Object.entries(response.data.data));
return Object.entries(response.data.data);
})
.then((employee) => {
console.log(employee)
res.render("pages/directory", {
employees: employee
});
})
.catch(function (error) {
console.log(error);
});
});
// Single Employee
// "Render" the person view here!
app.get("/directory/:uid", function (req, res) {
let id = req.params.uid;
var config = {
method: 'get',
url: `https://spa-lab-project-ii-default-rtdb.firebaseio.com/data/${id}.json`,
headers: { }
};
axios(config)
.then((response) => {
let dataFromAPI = response.data;
return dataFromAPI
})
.then((response) => {
res.render("pages/person",{
employee: response });
})
.catch(function (error) {
console.log(error);
});
});
// DELETE user
app.get("/delete/:uid", function (req, res) {
let id = req.params.uid;
var config = {
method: 'delete',
url: `https://spa-lab-project-ii-default-rtdb.firebaseio.com/data/${id}.json`,
headers: { }
};
axios(config)
.then((response) => {
let dataFromAPI = response.data;
return dataFromAPI
})
.catch(function (error) {
console.log(error);
});
res.redirect("/directory");
});
//UPDATE User
app.post("/update", function(req, res) {
let pattern = /-\w{3,}\D\w+/g;
let idLocation = req.headers.referer;
let id = pattern.exec(idLocation)
//Our RegEx is used to grab the id from within the object Headers: Referer
var data = `{"firstName":"${req.body.user.firstName}"}`;
var config = {
method: 'patch',
url: `https://spa-lab-project-ii-default-rtdb.firebaseio.com/data/${id}.json`,
headers: {
"Content-Type": "text/plain"
},
data : data
};
axios(config)
.then(function (response) {
})
.catch(function (error) {
console.log(error);
});
res.redirect("/directory");
});
app.get("/update/:uid", function (req, res) {
let id = req.params.uid;
var config = {
method: 'get',
url: `https://spa-lab-project-ii-default-rtdb.firebaseio.com/data/${id}.json`,
headers: { }
};
axios(config)
.then((response) => {
let dataFromAPI = response.data;
return dataFromAPI
})
.then((response) => {
res.render("pages/update",{
employee: response });
})
.catch(function (error) {
console.log(error);
});
});
// Express's .listen method is the final part of Express that fires up the server on the assigned port and starts "listening" for request from the app! (boilerplate code from the docs)
app.listen(2001);
console.log("Port 2001 is open");