DEV Community

OlammieAO
OlammieAO

Posted on

Reactjs NodeJS Cors Request Not Succeeded Errors

My reactjs website runs on https and my NodeJS server is https but I got error calling the NodeJS api from reactjs website. Both ReactJS website and NodeJS api deployed on the same linux VPS server, my database is on a remote but the connection is fine as I am not getting database connection error but i got cors errors like below:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://localhost:8184/api/v1/signin. (Reason: CORS request did not succeed). Status code: (null).

My NodeJS that started the https server enable cors and it allowed cross origin. See my NodeJS

const cors = require('cors')({
   origin: 'https://mydomaindotcom'
});


const allowCrossDomain = function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'POST, GET, PUT,PATCH, DELETE, OPTIONS, ');
    res.header('Access-Control-Allow-Credentials', false);
    res.header('Access-Control-Max-Age', '86400');
    res.header('Access-Control-Allow-Headers', 'Authorization, X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
    next();
};
app.use(allowCrossDomain);
// Error logger
app.use(logger('dev', {
    skip: function (req, res) { return res.statusCode < 400 },
    stream: fileWriter
}))
app.use(logger('dev'));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(cors);
app.use(fileUpload());
app.use("/api/v1", routes);
Enter fullscreen mode Exit fullscreen mode

In my ReactJS package.json file i have my domain as homepage like below:

"homepage": "https://mydomain",
"Hostname": "https://mydomain",
Enter fullscreen mode Exit fullscreen mode

Please help on this cors errors, it has taken enough sweat on my face.

Top comments (0)