How to fix CORS ('Access-Control-Allow-Origin') error in Meteor app?

In my case I’m using it with Apollo and GraphQL. Here’s additional code in response to your question:

const GRAPHQL_PORT = 3010;
const SUBSCRIPTION_PORT = 8080;
const graphQLServer = express();

//TAKE CARE OF CORS
//http://stackoverflow.com/a/33483759/364966
var whitelist = [
    'http://localhost:3000',
    'http://10.0.1.11:3000',
];
var corsOptions = {
    origin: function(origin, callback){
        var originIsWhitelisted = whitelist.indexOf(origin) !== -1;
        callback(null, originIsWhitelisted);
    },
    credentials: true
};
graphQLServer.use(cors(corsOptions));
//END OF CODE TO TAKE CARE OF CORS

This way the GraphQL server accepts cross-origin calls from localhost:3000, i.e. my Meteor app.

It’s ExpressJS implementation, not for Meteor server

I would love a Meteor answer to this… did you find one? @nadeemjq

2 Likes

Thanks for the post - This worked for me, when calling from a cordova mobile app running in the browser, with the app at http://127.0.0.1:8081 I was getting 405 responses trying to call 127.0.0.1:3000 where my Meteor server was running. I had to whitelist that exact URL to be able to call meteor via graphql (using apollo-vue) due to the CORS restrictions in place.

who can anwers help me how to fix It on meteor client? Please!

3 Likes

Did anyone manage to find a solution to this? I need help.

Yes, I suggest you search via Google and Stackoverflow as this has nothing to do with Meteor.

In my own case it was fixed on AWS S3 as the settings there were causing it

Which is your case?

  1. you are calling an API built in Meteor from “outside” and you get CORS errors from the 3rd party you call from.
  2. you are calling from a Meteor Client to a 3rd party service (perhaps using fetch())
  3. Like 2 but calling from the Meteor server instead.

Thank you for the support, I was able to solve this, the problem was I was calling from the Meteor client to a 3rd party instead of calling from the Meteor server.