question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Apollo Server Express - CORS issue

See original GitHub issue

According to the Apollo docs for Express, the way to enable CORS is in the .applyMiddleware({app}) function I have tested this and it doesn’t work. My setup is exactly the same as this: https://github.com/apollographql/apollo-server/tree/master/packages/apollo-server-express#express except that the applyMiddleware now looks like this:

server.applyMiddleware({
  app,
  cors: {
    credentials: true,
    origin: new RegExp("/*/")
  }
})

Still, on my frontend I get a CORS error:

“Access to fetch at ‘https://blablabla.now.sh/graphql’ from origin ‘http://localhost:3000’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: 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.”

I’m going crazy trying to figure this out - any ideas how to make it work?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
miliu99commented, Apr 25, 2020

OK, I got it working now. It turns out the error message is completely unrelated to my problem - it has nothing to do with CORS. In my case, it had included a file from wrong path. If I had looked at the CloudWatch log, I would have figured it out right away, but I was misled by the message and looking for CORS all the time. I could have deleted my previous post, but i think it might help somebody down the road running into similar problems. So, check the CloudWatch log first!

3reactions
abernixcommented, Mar 18, 2020

I don’t believe this is a problem with Apollo Server. The express integration directly passes the options to cors:

https://github.com/apollographql/apollo-server/blob/53f9021923f0cb74c5df730f38e1217e85a9ffed/packages/apollo-server-express/src/ApolloServer.ts#L174-L179

Please consider the suggestion from @nikhilunni511 or investigate the problem more generically outside of the context of Apollo Server itself, or provide a reproduction indicating that it’s unique to Apollo Server’s usage of the cors module.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring CORS - Apollo GraphQL Docs
Cross-Origin Resource Sharing (CORS) is an HTTP-header-based protocol that enables a server to dictate which origins can access its resources.
Read more >
apollo-server-express CORS issue - node.js - Stack Overflow
So I am migrating to apollo-server-express 2.3. 3 ( I was using 1.3. 6 ) I've followed several guides, making the necessary tweaks...
Read more >
[Solved] apollo-server-express cors not handling #3363 - GitHub
Problem Kept receiving cors errors with many possible configurations Solution ... There was no issue with the apollo server or express.
Read more >
How to enable CORS for apollo-server | rockyourcode
Cross-Origin Resource Sharing (CORS) is a mechanism that uses additional HTTP headers to tell a browser to let a web application running at...
Read more >
Enabling CORS for Express-GraphQL & Apollo Server - Prisma
CORS is an important protection mechanism preventing websites from downloading malicious resources, but from a developer standpoint it can be a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found