I am trying to setup a Node API on AWS Elastic Beanstalk. On the front-end I have a React App spun up on AWS Amplify. Now, when I try to fetch a register or signup hitting my API on a custom subdomain setup I get the following error in the front-end console.
Access to fetch at 'https://api.mydomain.com/login' from origin 'https://app.mydomain.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.
I have tried to do everything possible but haven't been able to run it.
Could someone please help?
const express = require("express");
const cors = require("cors");
// const nodemon = require("");
// const auth = require("./auth");
//Web Server
const app = express();
const port = process.env.PORT;
const http = require('http').Server(app);
//Web Server - Optimization
app.use(cors());
app.options('*', cors());
app.use(bodyParser.json({limit: '10mb', extended: true}));
app.use(bodyParser.urlencoded({limit: '10mb', extended: true}));
const corsOptions = {
origin: ['https://app.mydomain.com']
}
app.use(cors(corsOptions))
app.use((req,res,next)=>{
res.setHeader('Access-Control-Allow-Origin','*');
res.setHeader('Access-Control-Allow-Methods','GET,POST,PUT,PATCH,DELETE');
res.setHeader('Access-Control-Allow-Methods','Content-Type','Authorization');
next();
})
const Jwt = require('jsonwebtoken');
const jwtKey = 'th-connect';
const bodyParser = require('body-parser');
require('./db/config');
const User = require('./models/Users');
//Health Check
app.get('/', (req, res) => {
res.send('Server is Online - Version 2.0 running on port ' + port);
});