I am trying to configure a proxy server ( setupProxy.js ) within a create-react-app using HTTP-proxy-middleware to get access to a weather data API ( api.darksky.net ).
I followed the steps within the React documentation ( https://facebook.github.io/create-react-app/docs/proxying-api-requests-in-development#configuring-the-proxy-manually ) but am still having issues with CORS.
I have tried prepending my API URL within my fetch call with 'https://cors-anywhere.herokuapp.com/' ( https://github.com/Rob--W/cors-anywhere/ ) and this is working, but it feels a little corny to me and I'd rather get this working on my own.
Here is the function that is ultimately being called from within componentDidMount:
fetchWeatherDataAuto = () => {
let lat = this.state.locInfo.lat;
let lon = this.state.locInfo.lon;
fetch(`https://api.darksky.net/forecast/${apiKey.darkSky_key}/${lat},${lon}`)
.then(response => response.json())
.then(response => console.log("Weather Response: ", response));
}
Here is the code that is my setupProxy.js file:
const proxy = require('http-proxy-middleware');
module.exports = function(app) {
app.use(proxy("/forecast", {
target: "https://api.darksky.net/",
changeOrigin: true
}));
}
This error is shown in my console:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading >the remote resource at >https://api.darksky.net/forecast/{myAPIKey}/9.739>9056,-82.8484079. (Reason: CORS header ‘Access-Control-Allow-Origin’ >missing).