-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
94 lines (79 loc) Β· 3.13 KB
/
server.js
File metadata and controls
94 lines (79 loc) Β· 3.13 KB
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
const express = require('express');
const path = require('path');
const cors = require('cors');
const dotenv = require('dotenv');
const https = require('https');
dotenv.config();
const app = express();
const PORT = process.env.PORT || 3000;
// Enable CORS
app.use(cors());
// Serve static files
app.use(express.static(path.join(__dirname, 'public')));
// Helper function for HTTPS requests
const makeHttpsRequest = (url) => {
return new Promise((resolve, reject) => {
https.get(url, (res) => {
let data = '';
res.on('data', (chunk) => (data += chunk));
res.on('end', () => resolve(JSON.parse(data)));
res.on('error', reject);
}).on('error', reject);
});
};
app.get("/favicon.ico", (req, res) => {
res.status(204).end(); // Respond with "No Content" to prevent errors
});
// API endpoints
app.get('/api/weather-key', (req, res) => {
if (!process.env.WEATHER_API_KEY) {
return res.status(500).json({ error: 'Weather API key not configured' });
}
res.json({ WEATHER_API_KEY: process.env.WEATHER_API_KEY });
});
app.get('/api/geocode', async (req, res) => {
const { city } = req.query;
if (!city) return res.status(400).json({ error: 'City parameter is required' });
try {
const url = `https://api.geoapify.com/v1/geocode/search?text=${encodeURIComponent(city)}&format=json&apiKey=${process.env.GEOAPIFY_API_KEY}`;
const data = await makeHttpsRequest(url);
res.json(data);
} catch (error) {
console.error('Geocoding error:', error.message);
res.status(500).json({ error: 'Failed to fetch geocoding data' });
}
});
app.get('/api/places', async (req, res) => {
const { lon, lat } = req.query;
if (!lon || !lat) return res.status(400).json({ error: 'Longitude and latitude are required' });
try {
const url = `https://api.geoapify.com/v2/places?categories=tourism.sights&filter=circle:${lon},${lat},50000&limit=6&sort=importance&apiKey=${process.env.GEOAPIFY_API_KEY}`;
const data = await makeHttpsRequest(url);
res.json(data);
} catch (error) {
console.error('Places error:', error.message);
res.status(500).json({ error: 'Failed to fetch places data' });
}
});
app.get('/api/destination-images', async (req, res) => {
const { query } = req.query;
if (!query) return res.status(400).json({ error: 'Query parameter is required' });
try {
const url = `https://api.unsplash.com/search/photos?query=${encodeURIComponent(query)}&per_page=15&client_id=${process.env.UNSPLASH_API_KEY}`;
const data = await makeHttpsRequest(url);
res.json(data.results);
} catch (error) {
console.error('Error fetching images:', error.message);
res.status(500).json({ error: 'Failed to fetch images' });
}
});
app.get('/api/maps-key', (req, res) => {
if (!process.env.GOOGLE_MAPS_API_KEY) {
return res.status(500).json({ error: 'Google Maps API key not configured' });
}
res.json({ apiKey: process.env.GOOGLE_MAPS_API_KEY });
});
// Start the server
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});