forked from osu-cs340-ecampus/nodejs-starter-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
204 lines (164 loc) · 5.93 KB
/
app.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
/*
SETUP
*/
// Express
var express = require('express');
var app = express();
app.use(express.json())
app.use(express.urlencoded({extended: true}))
PORT = 9125;
// Database
var db = require('./database/db-connector');
// Handlebars
var exphbs = require('express-handlebars');
const { query } = require('express');
app.engine('.hbs', exphbs({
extname: ".hbs"
}));
app.set('view engine', '.hbs');
// Static Files
app.use(express.static('public'));
/*
ROUTES
*/
// GET ROUTES
app.get('/', function(req, res)
{
// Declare Query 1
let query1;
// If there is no query string, we just perform a basic SELECT
if (req.query.lname === undefined)
{
query1 = "SELECT * FROM bsg_people;";
}
// If there is a query string, we assume this is a search, and return desired results
else
{
query1 = `SELECT * FROM bsg_people WHERE lname LIKE "${req.query.lname}%"`
}
// Query 2 is the same in both cases
let query2 = "SELECT * FROM bsg_planets;";
// Run the 1st query
db.pool.query(query1, function(error, rows, fields){
// Save the people
let people = rows;
// Run the second query
db.pool.query(query2, (error, rows, fields) => {
// Save the planets
let planets = rows;
// Construct an object for reference in the table
// Array.map is awesome for doing something with each
// element of an array.
let planetmap = {}
planets.map(planet => {
let id = parseInt(planet.id, 10);
planetmap[id] = planet["name"];
})
// Overwrite the homeworld ID with the name of the planet in the people object
people = people.map(person => {
return Object.assign(person, {homeworld: planetmap[person.homeworld]})
})
return res.render('index', {data: people, planets: planets});
})
})
});
// POST ROUTES
app.post('/add-person-ajax', function(req, res)
{
// Capture the incoming data and parse it back to a JS object
let data = req.body;
// Capture NULL values
let homeworld = parseInt(data.homeworld);
if (isNaN(homeworld))
{
homeworld = 'NULL'
}
let age = parseInt(data.age);
if (isNaN(age))
{
age = 'NULL'
}
// Create the query and run it on the database
query1 = `INSERT INTO bsg_people (fname, lname, homeworld, age) VALUES ('${data.fname}', '${data.lname}', ${homeworld}, ${age})`;
db.pool.query(query1, function(error, rows, fields){
// Check to see if there was an error
if (error) {
// Log the error to the terminal so we know what went wrong, and send the visitor an HTTP response 400 indicating it was a bad request.
console.log(error)
res.sendStatus(400);
}
else
{
// If there was no error, perform a SELECT * on bsg_people
query2 = `SELECT * FROM bsg_people;`;
db.pool.query(query2, function(error, rows, fields){
let people = rows;
// If there was an error on the second query, send a 400
if (error) {
// Log the error to the terminal so we know what went wrong, and send the visitor an HTTP response 400 indicating it was a bad request.
console.log(error);
res.sendStatus(400);
}
// If all went well, send the results of the query back.
else
{
// If there was no error, perform a SELECT * on bsg_planets
query3 = `SELECT * FROM bsg_planets;`;
db.pool.query(query3, function(error, rows, fields){
// Save the planets
let planets = rows;
// Construct an object for reference in the table
// Array.map is awesome for doing something with each
// element of an array.
let planetmap = {}
planets.map(planet => {
let id = parseInt(planet.id, 10);
planetmap[id] = planet["name"];
})
people = people.map(person => {
return Object.assign(person, {homeworld: planetmap[person.homeworld]})
})
res.send(people);
})
}
})
}
})
});
app.post('/add-person-form', function(req, res){
// Capture the incoming data and parse it back to a JS object
let data = req.body;
// Capture NULL values
let homeworld = parseInt(data['input-homeworld']);
if (isNaN(homeworld))
{
homeworld = 'NULL'
}
let age = parseInt(data['input-age']);
if (isNaN(age))
{
age = 'NULL'
}
// Create the query and run it on the database
query1 = `INSERT INTO bsg_people (fname, lname, homeworld, age) VALUES ('${data['input-fname']}', '${data['input-lname']}', ${homeworld}, ${age})`;
db.pool.query(query1, function(error, rows, fields){
// Check to see if there was an error
if (error) {
// Log the error to the terminal so we know what went wrong, and send the visitor an HTTP response 400 indicating it was a bad request.
console.log(error)
res.sendStatus(400);
}
// If there was no error, we redirect back to our root route, which automatically runs the SELECT * FROM bsg_people and
// presents it on the screen
else
{
res.redirect('/');
}
})
})
/*
LISTENER
*/
app.listen(PORT, function(){
console.log('Express started on http://localhost:' + PORT + '; press Ctrl-C to terminate.')
});