-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
160 lines (122 loc) · 5.49 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
var http = require('http');
var express = require('express') //handles client requests
var mysql = require('mysql'); //enables working with mySQL database
var bodyParser = require('body-parser');
var app = express()
app.use(express.static("."));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
var covid_location;
var covid_date;
//------------------------- Database connection -------------------------------
//create connection
var db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password', //ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'
database: 'covid'
});
db.connect(function (err) {
if (!err) {
console.log("Database is connected...")
}
else {
console.log("Error while connecting with database");
}
});
//-----------------------Adding new location ----------------------------------
app.get('/addListPage', function (req, res) {
console.log("Rending add list page...")
var tabl_html = '<h2>its working</h2>'
var table_html = `
<button class="button button4" onclick="addRestaurant();">Add Restaurant</button>`
res.send(table_html);
});
app.post('/addLocation', function (req, res) {
console.log("Processing add...")
var user = "Tutu";
var location = db.escape(req.body.Location);
var date = db.escape(req.body.Date);
console.log('Username:' + user + ' Location: ' + location + ' Date: ' + date);
sql_cmd = `INSERT INTO
locations(name, location, date)
VALUES
("` + user + `",` + location + `,` + date + `); `
//insert into locations(name, location, date) values("Dila", "Upenn", "2020-08-02");
//update lacation table with data
db.query(sql_cmd, function (err, rows, fields) {
if (err) throw err;
//res.send("New Location Added!");
});
});
//----------------- Load Location ---------------------------------------------
//set the values that will be used in select statement later
app.post('/testLocation', function (req, res) {
var user = "Tutu";
covid_location = req.body.Location;
covid_date = req.body.Date;
console.log('Username:' + user + ' Location: ' + covid_location + ' Date: ' + covid_date);
//res_html = '<button onClick = "loadLocation();">load data</button>'
res.send("cool");
});
//generate the user's list from database
app.get('/loadLocation', function (req, res) {
var user = "Tutu";
//var location = db.escape(req.body.Location);
//var date = db.escape(req.body.Date);
//console.log('-----Username:' + user + ' Location: ' + covid_location + ' Date: ' + covid_date);
var location = covid_location;
var date = covid_date;
console.log('Username:' + user + ' Location: ' + location + ' Date: ' + date);
db_cmd = `SELECT name, location, date FROM locations WHERE location = "` + covid_location + `" AND date = "` + covid_date + `";`
//select name, location, date from locations where location = "Upenn";
db.query(db_cmd, function(err, rows, fields){
if (err) throw err;
console.log("Generating user's table");
var table_html = `<body><div><a class="exit" href="loadlocation.html">Exit</a><div style = "width: 100%; margin-top: 4rem;"><table border= "1" rules="rows" id="list"><tr>`
var headers = [];
for( i = 0; i < fields.length; i++){
headers.push(fields[i].name);
table_html += '<th>' + fields[i].name + '</th>';
}
table_html += '</tr>';
for( i = 0; i < rows.length; i++){
table_html += '<tr>'
for( j = 0; j < headers.length; j++){
table_html += '<td>' + rows[i][headers[j]] + '</td>';
}
table_html += '</tr>';
}
table_html += '</table></div></div></body>';
table_html += '<div class="navbar"><a href="addlocation.html"><i class="fa fa-home"></i></a><a href="#"><i class="fa fa-list"></i></a><a href="#"><i class="fa fa-thermometer-quarter"></i></a><a href="#"><i class="fa fa-users"></i></a><a href="#"><i class="fa fa-user-circle"></i></a></div>';
res.send(table_html);
});
});
app.get('/myLocations', function (req, res) {
var user = "Tutu";
db_cmd = `SELECT name, location, date FROM locations WHERE name = "` + user + `";`
db.query(db_cmd, function(err, rows, fields){
if (err) throw err;
console.log("Generating tutu's table");
var table_html = `<body><div><a class="exit" href="addlocation.html">Exit</a><div style = "width: 100%; margin-top: 4rem;"><table border= "1" rules="rows" id="list"><tr>`
var headers = [];
for( i = 0; i < fields.length; i++){
headers.push(fields[i].name);
table_html += '<th>' + fields[i].name + '</th>';
}
table_html += '</tr>';
for( i = 0; i < rows.length; i++){
table_html += '<tr>'
for( j = 0; j < headers.length; j++){
table_html += '<td>' + rows[i][headers[j]] + '</td>';
}
table_html += '</tr>';
}
table_html += '</table></div></div></body>';
table_html += '<div class="navbar"><a href="addlocation.html"><i class="fa fa-home"></i></a><a href="#"><i class="fa fa-list"></i></a><a href="#"><i class="fa fa-thermometer-quarter"></i></a><a href="#"><i class="fa fa-users"></i></a><a href="#"><i class="fa fa-user-circle"></i></a></div>';
res.send(table_html);
});
});
app.listen(8080, function () {
console.log('Server Running');
});