Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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();
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!