samedi 5 janvier 2019

Node.js with Express randomly doesn't return Access-Control-Allow-Origin header

My express application has a middleware defined in this way:

application.use((req: Request, res: Response, next: NextFunction) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
    res.header('Content-Type', 'application/json');
    next();
  });

When I reach my endpoint from react app quite often (let's say 6 times per 10 seconds), using axios or fetch api, 10% of responses doesn't have a Access-Control-Allow-Origin header and my front end app simply fails with the following error:

Access to XMLHttpRequest at 'X' from origin 'Y' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I reach my service on digital ocean via Apache with certbot configuration (not sure if it matters).

How can I fix this issue, so 100% of my responses have correct CORS headers?

Any advices whould be greatly appreciated!




Aucun commentaire:

Enregistrer un commentaire