Add Yelp Fusion API proxy endpoint for dynamic map search
This commit is contained in:
56
server.js
56
server.js
@@ -360,6 +360,62 @@ app.post('/api/polls', (req, res) => {
|
||||
res.json({ success: true, pollsOpen: data.pollsOpen });
|
||||
});
|
||||
|
||||
// ── Yelp Fusion API proxy ───────────────────────────────────
|
||||
// API key lives server-side — never exposed to the browser.
|
||||
// Get your free key at: https://www.yelp.com/developers
|
||||
const YELP_API_KEY = process.env.YELP_API_KEY || '';
|
||||
|
||||
app.get('/api/yelp', async (req, res) => {
|
||||
const { term, location } = req.query;
|
||||
if (!term) return res.status(400).json({ error: 'term is required' });
|
||||
|
||||
if (!YELP_API_KEY) {
|
||||
return res.status(503).json({
|
||||
error: 'YELP_API_KEY not configured on server. Add it as an environment variable.'
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const params = new URLSearchParams({
|
||||
term: term + ' in ' + location,
|
||||
location: location || 'Los Cabos Mexico',
|
||||
limit: '15',
|
||||
sort_by: 'rating',
|
||||
categories: 'restaurants,nightlife,active,arts,health',
|
||||
});
|
||||
const response = await fetch(`https://api.yelp.com/v3/businesses/search?${params}`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${YELP_API_KEY}`,
|
||||
'Accept': 'application/json',
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errText = await response.text();
|
||||
return res.status(response.status).json({ error: `Yelp API error: ${errText}` });
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
// Return only the fields we need to keep payload small
|
||||
const businesses = (data.businesses || []).map(b => ({
|
||||
name: b.name,
|
||||
image_url: b.image_url,
|
||||
url: b.url,
|
||||
rating: b.rating,
|
||||
price: b.price,
|
||||
coordinates: b.coordinates,
|
||||
location: b.location,
|
||||
categories: b.categories,
|
||||
display_phone: b.display_phone,
|
||||
distance: b.distance,
|
||||
}));
|
||||
res.json({ businesses, total: data.total });
|
||||
} catch (err) {
|
||||
console.error('Yelp proxy error:', err);
|
||||
res.status(500).json({ error: 'Failed to fetch from Yelp' });
|
||||
}
|
||||
});
|
||||
|
||||
// ── WebSocket ────────────────────────────────────────────────
|
||||
|
||||
wss.on('connection', (ws) => {
|
||||
|
||||
Reference in New Issue
Block a user