CORS Issues when calling API
I have a pretty simple web app which pings the Robinhood API for stock quotes. This is a specific part of their API that doesn't rely on any auth pieces and I have no issue loading the JSON responses in my browser by just typing the URLs.
E.G: https://api.robinhood.com/quotes/AAPL/ brings up everything I need.
However when I deploy my build to Heroku (with a simple node/express server) I get CORS errors in all API calls, that look like this:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin [APP-URL] is therefore not allowed access.
BTW I'm using axios for my API calls.
I've seen these errors before and have a basic understanding of the issue, which led me to adding the following to my server.js file:
app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); next(); });
It doesn't seem to have fixed the issue however. In fact it had no effect. Being pretty new to this, I'm not super knowledgeable on the server setup, and thus am not sure what to do next. I'm wondering if perhaps my actual get requests need to have a specific parameter that I'm missing.
Any help would be most appreciated!
FYI my entire server.js file looks like this:
const express = require('express'); const path = require('path'); const serveStatic = require('serve-static');
app = express();
app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); next(); });
app.use(serveStatic(__dirname));
const port = process.env.PORT || 5000;
app.listen(port);
console.log('server started '+ port);
Posted Jul 16, 2017, 9:25 PM UTC