-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
886342b
commit a4c2745
Showing
2 changed files
with
58 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,35 @@ | ||
const axios = require('axios'); | ||
|
||
module.exports = (req, res) => { | ||
const apiKey = process.env.REACT_APP_API_KEY; // Make sure this environment variable is correctly set in your deployment | ||
const { term, location, sort_by } = req.query; | ||
module.exports = async (req, res) => { | ||
// Set CORS headers to allow all domains or specify your application's domain | ||
res.setHeader('Access-Control-Allow-Origin', '*'); // Adjust accordingly for production | ||
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'); | ||
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization'); | ||
|
||
// Optional: Handle pre-flight requests for CORS | ||
if (req.method === 'OPTIONS') { | ||
res.status(200).end(); | ||
return; | ||
} | ||
|
||
console.log('API Key:', apiKey); // Debugging: Log the API Key | ||
console.log('Request query params:', req.query); // Debugging: Log the request query parameters | ||
const apiKey = process.env.REACT_APP_API_KEY; // Ensure this is set in your Vercel project settings | ||
const { term, location, sort_by } = req.query; | ||
|
||
axios({ | ||
method: 'get', | ||
url: `https://api.yelp.com/v3/businesses/search`, | ||
headers: { | ||
Authorization: `Bearer ${apiKey}`, | ||
}, | ||
params: { // Use the params object for query parameters | ||
term: term, | ||
location: location, | ||
sort_by: sort_by, | ||
}, | ||
}) | ||
.then((response) => { | ||
console.log('Yelp API response:', response.data); // Debugging: Log Yelp API response | ||
res.send(response.data); | ||
}) | ||
.catch((error) => { | ||
console.log('Yelp API error:', error); // Debugging: Log any error from the Yelp API | ||
try { | ||
const { data } = await axios.get(`https://api.yelp.com/v3/businesses/search`, { | ||
params: { term, location, sort_by }, | ||
headers: { | ||
Authorization: `Bearer ${apiKey}`, | ||
}, | ||
}); | ||
res.status(200).json(data); | ||
} catch (error) { | ||
if (error.response) { | ||
// Forward Yelp API's error response | ||
res.status(error.response.status).send(error.response.data); | ||
res.status(error.response.status).json(error.response.data); | ||
} else { | ||
// Handle network or other errors not directly returned from Yelp's API | ||
res.status(500).send({ message: 'An error occurred while contacting the Yelp API' }); | ||
// Handle network or other errors | ||
res.status(500).json({ message: 'An error occurred while contacting the Yelp API' }); | ||
} | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a4c2745
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
ravenouseats – ./
ravenouseats-vincentphung.vercel.app
ravenouseats-git-codespace-vincentphung.vercel.app
a4c2745
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
ravenous – ./
ravenous-psi.vercel.app
ravenous-git-codespace-vincentphung.vercel.app
ravenous-vincentphung.vercel.app