Search code examples
reactjsnode.jssql-servercors

Cannot connect to nodejs server from client


I was in need for some help regarding my nodeJS server. I have my Windows Server which is running my frontend and server. My server is in nodeJs and it has all the api i made. This is the index.js of my server.

app.use(
    cors({
    origin: "https://localhost:8080",
    credentials: true,
    methods: ["GET", "POST", "OPTIONS", "PUT", "PATCH", "DELETE"],
    optionsSuccessStatus: 200,
    allowedHeaders: [
    "Access-Control-Allow-Origin",
    "content-type",
    "Cookies",
    "cookies",
    "Authorization",
    ],
    })
);

The thing is that I can access my API for userlisting by http://158.53.70.6:5000/users from my server itself in chrome and it works. But now i want to actually access it from another PC. How can I do this. Also a side note the frontend is hosted on open internet on https://172.23.50.5:5553/. The frontend on my PC is working but I can't access the data in it that is it cant access the API. Also just a side note if it is important, the db is hosted in MySQL phpmyadmin using Apache on port 80 and 443 and MySQL on port 3306.

This is the error:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at localhost:5000/users. (Reason: CORS header ‘Access-Control-Allow-Origin’ does not match ‘localhost:3000’).


Solution

  • so the thing was that it accessed my frontend but not the server as the server did not have open url link . So for the server i created another IP DNS just the same as https://172.23.50.5:5553/ but port was 5552. So since the server was also on open internet i could easily access the frontend data as well as just the server itself from url. Just ask you ict cloud team to provision another open url for server. But also an important note, secure the server as it is hosted free on open url.