0

I am in a team doing a client-server application. We are using Node.js (v12.18) in the backend as an API, and React (v16.13) for the frontend. Recently I've found this bug related to the CORS policy. I'm trying to send a POST request, deleting one resource in the database and when trying to delete it, this is the error I get:

Access to fetch at 'http://localhost:8080/clientes/eliminar' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

As far as I researched in forums and discussions I always find the same resolution, which is using the "Access-Control-Allow" headers, but we are already using them. I tried to change the POST method by a DELETE method when doing the request, but I find the same issue.

The headers that we are currently using in the backend, in case someone wonders what we have.

app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Credentials", "true");
    res.header("Access-Control-Allow-Methods", "GET, POST");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, token");
    next();
});

The sites I visited in order to solve the problem were pretty much the same as this one

TYSM for reading until here and ask for more information you need. Every piece of help is appreciated.

  • 1
    Most likely, there is an error occurring before your middleware for CORS is even running. Please post the request and response headers so we can take a look. – Brad Sep 03 '20 at 22:20
  • You probably want to Install and use the npm cors package, and see in particular the part of the docs at https://www.npmjs.com/package/cors#enabling-cors-pre-flight. The code shown in the question has no handling for the CORS preflight OPTIONS request. Handling just for the "Access-Control-Allow-\*" response headers isn’t sufficient on its own to ensure the server responds correctly to the CORS preflight OPTIONS request. – sideshowbarker Sep 03 '20 at 22:34

1 Answers1

0

Have you already tried to add CORS to the project and use it as a middleware?

//...

const cors = require('cors')

//...


app.use(cors())
gabriel_tiso
  • 1,007
  • 3
  • 11
  • 27