Cannot fetch between Express and React apps due to CORS Cannot fetch between Express and React apps due to CORS express express

Cannot fetch between Express and React apps due to CORS


Update:

res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8080');

With:

res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');


I encourage you to do app.use(cors()); before those lines

app.disable('x-powered-by');app.use(function(req, res, next) {  // Website you wish to allow to connectres.setHeader('Access-Control-Allow-Origin', 'http://localhost:8080');...

Express stacks those functions lets say as a "array" and because of that the order matters

Hope it resolve your problem.


Bit late to the party but just going to leave my answer here incase someone else has the same problem.

The idea is to allow CORS request to your Express server. Go to the directory with your server.js file and run:

npm install cors

Then inside server.js add the following lines:

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