0

I'm trying to figure out why when I make a call to my "API" (nodejs express) from my React client the OPTIONS, preflight request is executed first.

Server api run on http://localhost:8000 Client run on http://localhost:3000

In my React client I configured http-proxy-middleware as follows:

const { createProxyMiddleware } = require('http-proxy-middleware');

module.exports = function (app) {
    app.use(
        '/api',
        createProxyMiddleware({
            target: 'http://localhost:8000',
            changeOrigin: true,
        })
    );
};

Also the Axios instance is created with following options

const options = {
    baseURL: BASE_URL,
    timeout: 300000,
    withCredentials: false,
    headers: {
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin': '*',
    }
};
axios.create(config);

The Node.js(Express) BackEnd application is configured as follow:

app.use(cors(
    {
        "origin": "*", // just for test...
        "methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
        "allowedHeaders": "*" // just for test
    }
));

When I make a Post for instance for server-side token validation a preflight OPTIONS request is made. How can I avoid this ? Am I doing something wrong?

Thank you all.

LucaT
  • 333
  • 5
  • 13
  • There is nothing wrong about the options preflight request, since you are making request on a different domaine, you can have more detail info on this topic : https://stackoverflow.com/questions/29954037/why-is-an-options-request-sent-and-can-i-disable-it – ShueiYang Jan 23 '23 at 12:37
  • @ShueiYang said that..should I delete the http-proxy-middleware and just continue with standard cors() config? – LucaT Jan 23 '23 at 13:03
  • well maybe we have to ask the right question, is there a particular reason you want to remove those preflight request? or what's the real reason you want to use a proxy middleware to stand between the sender and the recipient ? – ShueiYang Jan 23 '23 at 14:13
  • Yes @ShueiYang, at the end you re right..I'm going to close the question an clarify some aspect I've asked above! Thanks. – LucaT Jan 25 '23 at 16:09

0 Answers0