Apollo-server-express sometimes return cors error

I’m having problem with CORS. My Node.js Server with apollo-server-express(version 3.7)

My problem is api server sometimes return cors error.

My React Project request api server and works well. But sometimes api server response this error Access to fetch at ‘https://api.xxxx.com/’ from origin ‘https://xxxx.com’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. If an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.

Why same client request sometime response cors error?

My Api Server run AWS LoadBalancer + Ec2 Instance 3.
This is about my issue?

I can’t speak english well.
Please understand me.

Thanks.

Can you share your Apollo Server configuration?
(copy and paste the entire part that says new ApolloServer({ ... })

Have you tried setting your origin?

const server = new ApolloServer({
  // ... your configuration
  cors: {
    origin: ["https://xxxx.com"]
  },
});

Thanks for your reply.

I use apollo-server-express.
And i use applyMiddleware and argument my express app.

This is my express app corsOptions.

export const corsOptions: CorsOptions = {
  origin:
    process.env.NODE_ENV === "development"
      ? "*"
      : [
          MySiteOriginList...
        ],
  credentials: true,
  optionsSuccessStatus: 200,
};

I have something question applyMiddleware have options cors.
But i am not set cors options.

Could it cause a problem?

Thanks!

I don’t quite understand what you mean. Your corsOptions looks correct. You then need to pass it to applyMiddleware.

server.applyMiddleware({
  // ... other options
  cors: corsOptions,
});

Are you saying you don’t currently pass the corsOptions anywhere right now?