Express.js CORS between servers

I have a small problem with the connection between my Expressjs API and the React client.

Express API -> http://localhost:3001 React -> http://exampleip:3000 (both are on the same windows server)

I added the package CORS and add the following code

const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());

... other code

app.use('/testdata', async function (req, res) {
    const data = await receiveData();
    res.send(data);
});

If I fetch the data with the react app on the server where the code is located, I receive the data from the express api without problems.

If I fetch the data with the ip from the react app on my local pc or on a citrix terminal session (windows) on the same network (but not on the same windows server), I receive the following errors.

Console error Screenshot

Network error Screenshot

Answer

i prefer to use this code it will help you

// init application
const app = express();

// fix access to back-end
app.use((req, res, next) => {
  // Website you wish to allow to connect
  res.setHeader("Access-Control-Allow-Origin", process.env.FRONTEND_APP_HOST);

  // Request methods you wish to allow
  res.setHeader(
    "Access-Control-Allow-Methods",
    "GET, POST, OPTIONS, PUT, PATCH, DELETE"
  );

  // Request headers you wish to allow
  res.setHeader(
    "Access-Control-Allow-Headers",
    "X-Requested-With,content-type"
  );

  // Set to true if you need the website to include cookies in the requests sent
  // to the API (e.g. in case you use sessions)
  res.setHeader("Access-Control-Allow-Credentials", true);

  // Pass to next layer of middleware
  next();
});