-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
239 lines (198 loc) · 7.97 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
import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import fetch from 'node-fetch';
const app = express();
const PORT = 3000;
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Serve static files from the 'public' directory
app.use(express.static(path.join(__dirname, 'public')));
// Send 'index.html' when the homepage is accessed
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'homepage.html'));
});
// Function to switch tabs, currently unused
function openTab(evt, tabName) {
var i, tabcontent, tabbuttons;
// Hide all tab contents
tabcontent = document.getElementsByClassName("tab-content");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
// Remove the active class from all tab buttons
tabbuttons = document.getElementsByClassName("tab-button");
for (i = 0; i < tabbuttons.length; i++) {
tabbuttons[i].className = tabbuttons[i].className.replace(" active", "");
}
// Show the current tab and mark the button as active
document.getElementById(tabName).style.display = "block";
evt.currentTarget.className += " active";
}
// Experimental code for interacting with a Flask API
// Endpoint to get data from Flask API
app.get('/api/flask', async (req, res) => {
try {
const response = await fetch('http://localhost:5000/api/data');
const data = await response.json();
res.json(data);
} catch (error) {
console.error('Error fetching from Flask API:', error);
res.status(500).json({ error: 'Failed to fetch data from Flask API' });
}
});
// Endpoint to send data to Flask API
app.post('/api/flask/send', async (req, res) => {
try {
const response = await fetch('http://localhost:5000/api/receive', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(req.body)
});
const data = await response.json();
res.json(data);
} catch (error) {
console.error('Error sending to Flask API:', error);
res.status(500).json({ error: 'Failed to send data to Flask API' });
}
});
// Endpoint to fetch test data from Flask API
app.get('/api/test-data', async (req, res) => {
try {
console.log('Fetching test data from Flask API at: http://localhost:5000/test/data');
const response = await fetch('http://localhost:5000/test/data');
if (!response.ok) {
throw new Error(`Flask API responded with status: ${response.status}`);
}
const data = await response.json();
console.log('Received data from Flask:', data);
res.json(data);
} catch (error) {
console.error('Error fetching from Flask API:', error);
res.status(500).json({
error: 'Failed to fetch test data from Flask API',
details: error.message
});
}
});
// Endpoint to fetch unique car makes from Flask API
app.get('/api/unique_car_makes', async (req, res) => {
try {
console.log('Fetching car makes from Flask API at: http://localhost:5000/api/unique_car_makes');
const response = await fetch('http://localhost:5000/api/unique_car_makes');
if (!response.ok) {
throw new Error(`Flask API responded with status: ${response.status}`);
}
const data = await response.json();
console.log('Received data from Flask:', data);
res.json(data);
} catch (error) {
console.error('Error fetching from Flask API:', error);
res.status(500).json({
error: 'Failed to fetch car makes from Flask API',
details: error.message
});
}
});
// Endpoint to fetch unique car makes from Flask API
app.get('/api/unique_car_models', async (req, res) => {
try {
console.log('Fetching car makes from Flask API at: http://localhost:5000/api/unique_car_models');
const response = await fetch('http://localhost:5000/api/unique_car_models');
if (!response.ok) {
throw new Error(`Flask API responded with status: ${response.status}`);
}
const data = await response.json();
console.log('Received data from Flask:', data);
res.json(data);
} catch (error) {
console.error('Error fetching from Flask API:', error);
res.status(500).json({
error: 'Failed to fetch car makes from Flask API',
details: error.message
});
}
});
// Endpoint to fetch unique car cities from Flask API
app.get('/api/unique_cities', async (req, res) => {
try {
console.log('Fetching car makes from Flask API at: http://localhost:5000/api/unique_cities');
const response = await fetch('http://localhost:5000/api/unique_cities');
if (!response.ok) {
throw new Error(`Flask API responded with status: ${response.status}`);
}
const data = await response.json();
console.log('Received data from Flask:', data);
res.json(data);
} catch (error) {
console.error('Error fetching from Flask API:', error);
res.status(500).json({
error: 'Failed to fetch car makes from Flask API',
details: error.message
});
}
});
// Endpoint to fetch unique car cities from Flask API
app.get('/api/unique_states', async (req, res) => {
try {
console.log('Fetching car makes from Flask API at: http://localhost:5000/api/unique_states');
const response = await fetch('http://localhost:5000/api/unique_states');
if (!response.ok) {
throw new Error(`Flask API responded with status: ${response.status}`);
}
const data = await response.json();
console.log('Received data from Flask:', data);
res.json(data);
} catch (error) {
console.error('Error fetching from Flask API:', error);
res.status(500).json({
error: 'Failed to fetch car makes from Flask API',
details: error.message
});
}
});
// Endpoint to fetch unique car cities from Flask API
app.get('/api/unique_titles', async (req, res) => {
try {
console.log('Fetching car makes from Flask API at: http://localhost:5000/api/unique_titles');
const response = await fetch('http://localhost:5000/api/unique_titles');
if (!response.ok) {
throw new Error(`Flask API responded with status: ${response.status}`);
}
const data = await response.json();
console.log('Received data from Flask:', data);
res.json(data);
} catch (error) {
console.error('Error fetching from Flask API:', error);
res.status(500).json({
error: 'Failed to fetch car makes from Flask API',
details: error.message
});
}
});
// Endpoint to fetch unique car cities from Flask API
app.get('/api/populate_choropleth_on_load', async (req, res) => {
try {
console.log('Fetching car makes from Flask API at: http://localhost:5000/api/populate_choropleth_on_load');
const response = await fetch('http://localhost:5000/api/populate_choropleth_on_load');
if (!response.ok) {
throw new Error(`Flask API responded with status: ${response.status}`);
}
const data = await response.json();
console.log('Received data from Flask:', data);
res.json(data);
} catch (error) {
console.error('Error fetching from Flask API:', error);
res.status(500).json({
error: 'Failed to fetch car makes from Flask API',
details: error.message
});
}
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});