Cannot connect to MongoDB atlas

Hey guys. I recently deployed my app on Netlify and so far, everything is pretty good; however I cannot connect to my mongoDB atlas. Can anyone guide me on how to connect my create-react-app, express server all to mongo atlas without running into CORS issues? It says the error “has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.” whenever I try to get a get request to my mongo atlas. I’m sure many have dealt with this issue and I’m sure there is a step by step guide; I have tried looking and cannot find one. Please help me out I’m so close to deploying!

Hey @mostwontiwill

So, if your Netlify site was https://awesome-cool.netlify.app you would need to add

Access-Control-Allow-Origin: https://awesome-cool.netlify.app

as a header to your backend.

I know this may sound dumb, but can you further explain where I would put this? Like can you tell me what exactly I would need to put and where?

I put :

res.setHeader('Access-Control-Allow-Origin', '*');

I also have the npm package cors installed and put (even though I think they do the same thing):

app.use(function (req, res, next) {

  res.setHeader('Access-Control-Allow-Origin', '*');


  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

  res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');


  res.setHeader('Access-Control-Allow-Credentials', true);

  next();
});

What else am I missing here? Pretty frustrating stuff.

A simpler solution is to use proxy rewrites: