-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.mjs
56 lines (41 loc) · 1.46 KB
/
server.mjs
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
import express from 'express';
import fetch from 'node-fetch';
import cors from 'cors';
import dotenv from 'dotenv';
dotenv.config();
const app = express();
const PORT = process.env.PORT || 5000;
app.use(cors());
app.get('/api/nearbyStores', async (req, res) => {
const { lat, lng, limit } = req.query;
const apiKey = process.env.GOOGLE_MAPS_API_KEY;
if (!apiKey) {
return res.status(500).json({ error: 'API key is not set' });
}
const fetchStores = async (keyword) => {
const url = `https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${lat},${lng}&radius=5000&keyword=${keyword}&key=${apiKey}`;
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Google API returned status ${response.status}`);
}
const data = await response.json();
if (data.status !== 'OK') {
throw new Error(`Google API error: ${data.status} - ${data.error_message}`);
}
return data.results;
};
try {
const guitarStores = await fetchStores('guitar');
let results = guitarStores;
if (limit) {
results = guitarStores.slice(0, parseInt(limit, 10));
}
res.json(results);
} catch (error) {
console.error('Error fetching nearby stores:', error);
res.status(500).json({ error: 'Error fetching nearby stores', details: error.message });
}
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});