-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
146 lines (123 loc) · 4.35 KB
/
index.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
require('dotenv').config();
const cors = require('cors');
const express=require("express");
const propertyRoutes = require('./routes/propertyRoutes');
const userRoutes = require('./routes/userRoutes');
const commentRoutes = require('./routes/commentRoutes');
const favoritesRoutes = require('./routes/favoritesRoutes');
const preferencesRoutes = require('./routes/preferencesRoutes');
const recommendationRoutes = require('./routes/recommendationRoutes');
const bucketListRoutes = require('./routes/bucketListRoutes');
const PORT = 8080||process.env.PORT;
const pool = require('./db');
const app = express();
const wellknown = require('wellknown');
//DO NOT TOUCH THIS CODE
const corsOptions = {
origin: ['http://localhost:3000',
'http://localhost:8081','https://geostate-frontend.vercel.app','https://www.geo.estate'],
//DO NOT TOUCH THIS CODE
credentials: true,
methods: 'GET, POST, PUT, DELETE, OPTIONS',
allowedHeaders: ['Authorization,Content-Type'],
maxAge: 3600
};
app.use(cors(corsOptions));
app.options('*', cors(corsOptions)); // Enable preflight OPTIONS request for all routes
//DO NOT TOUCH THIS CODE
// app.use((req, res, next) => {
// res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');
// res.setHeader('Access-Control-Allow-Credentials', 'true');
// res.setHeader('Access-Control-Allow-Methods', '*');
// res.setHeader('Access-Control-Allow-Headers', '*');
// res.setHeader('Access-Control-Max-Age', '3600');
// // Handle OPTIONS requests
// if (req.method === 'OPTIONS') {
// return res.status(204).send('');
// }
// next();
// });
app.use(express.json());
app.use('/api/users',userRoutes);
app.use('/api/properties', propertyRoutes);
app.use('/api/comments',commentRoutes);
app.use('/api/favorites', favoritesRoutes);
app.use('/api/preferences', preferencesRoutes);
app.use('/api/recommendations', recommendationRoutes);
app.use('/api/bucket-list', bucketListRoutes);
//testing
app.get('/', (req, res) => {
res.send('Hello, Hello, hello');
});
app.get('/api/message', (req, res) => {
const message = req.query.message || 'No message provided!';
res.send(`Your message: ${message}`);
});
app.get('/api/locations/:postalCode', async (req, res) => {
const postalCode = req.params.postalCode;
try {
// Check if postalCode is provided
const query =
`SELECT
zip_code,
population,
median_age,
median_age_male,
median_age_female,
male_pop,
female_pop,
vacancies_for_rent_percent,
vacancies_for_sale_percent,
home_value_forecast,
ST_AsText(geometry) AS geometry
FROM zip_codes WHERE zip_code = $1`
const result = await pool.query(query, [postalCode])
// If no results for the specified postal code
if (postalCode && result.rows.length === 0) {
return res.status(404).json({ message: 'No polygons found for this postal code' });
}
// Format the response with GeoJSON geometry
const locations = result.rows.map(row => ({
postal_code: row.postal_code,
geometry: wellknown.parse(row.geometry),
population: row.population,
medianAge: row.median_age,
medianAgeMale: row.median_age_male,
medianAgeFemale: row.median_age_female,
malePop: row.male_pop,
femalePop: row.female_pop,
vacanciesForRentPercent: row.vacancies_for_rent_percent,
vacanciesForSalePercent: row.vacancies_for_sale_percent,
homeValueForecast: row.home_value_forecast
}));
res.json(locations);
} catch (error) {
console.error('Error fetching data:', error);
res.status(500).json({ error: 'Failed to retrieve locations' });
}
});
app.get('/api/locations', async (req, res) => {
try {
const result = await pool.query(`
SELECT
zip_code,
CONCAT(city, ', ', state) AS name
FROM
zip_codes
WHERE
geometry IS NOT NULL;;
`);
// Format response
const formattedData = result.rows.map(row => ({
zipcode: row.zip_code,
name: row.name,
}));
res.json(formattedData);
} catch (error) {
console.error('Error fetching zipcodes:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});