I have made simple expressJS server with nodemailer and have hosted it on aws elastic beanstalk, after which I was provided a 'http' link for the same, but I entered that link in my website which was hosted on netlify, it threw an error stating that an 'https' website cannot access 'http' website, so I converted my aws link to 'https' link, but after that I got an cors error from my netlify website.
I've tried multiple things, including importing the cors library from express like this:
`
const corsOptions = {
origin: [
/\.netlify\.app$/,
],
methods: ['GET', 'POST']
}
app.use(cors(corsOptions));
`
or setting headers like:
`
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();
});
but these methods did not work either, I always get the same error:
access has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.`
Please Help me resolve this error, I want to enable cors.
CodePudding user response:
Thanks to Derviş Kayımbaşıoğlu for helping me.
If anyone is facing this issue, kindly check your load balancer settings, it requires a https redirect to http, so the port for https is '443' and for http(instance port) is '80'.
CodePudding user response:
I had the same issue,
But, I was dumb that i messed up with URL's.
If you're trying to make a call to API from CLIENT, make sure they both are either http / https. Don't mix them.(!Important)
This error arises when we are trying to make calls to a different web server other than you are working.