0

I'm encountering CORS (Cross-Origin Resource Sharing) errors in my web application, and I'm seeking help to troubleshoot and resolve this issue. Here are the key details of my setup:

  • Front-End: My web application uses React version 16.14.
  • Back-End: The API is built using Node.js version 16.19 with Express.js version 4.18.1.
  • Server Instance: Both the React front-end and Node.js API are hosted on a single AWS EC2 instance (t3.large).
  • Database: The application relies on a MySQL database hosted on AWS RDS.
  • Process Management: I use PM2 to manage services for both the React front-end and Node.js API.
  • Load Balancer: My setup includes an AWS Application Load Balancer.
  • Auto Scaling: I've configured an auto scaling group, so when the CPU utilization of the instance exceeds 35%, a new instance is automatically spun up to handle the load.

The issue I'm facing is that during specific peak traffic times, particularly at 9:30 AM, CORS errors occur. The rest of the time, the application runs smoothly without any problems.

I suspect that the CORS errors may be related to how my load balancer, auto scaling, or perhaps CORS settings are configured. I'm looking for guidance on how to:

  • Diagnose the CORS error during high traffic periods, particularly when the server is under load.
  • Understand the interplay between the AWS Application Load Balancer, auto scaling, and CORS.
  • Configure CORS settings to avoid these errors during peak traffic times.

I would appreciate any insights, tips, or advice on how to resolve this CORS issue and ensure a consistently smooth user experience, especially during high traffic periods.

Thank you in advance for your help!

  • CORS Configuration: I initially reviewed and adjusted the CORS configuration on the server-side (Node.js API) to accept requests from the domain of my React front-end. This included setting appropriate headers in the response.
  • Preflight Requests: I ensured that the API handles preflight requests (OPTIONS) correctly, and the responses include the necessary CORS headers.
  • AWS Load Balancer: I checked the AWS Application Load Balancer settings to ensure that it's correctly forwarding requests to my EC2 instance.
  • Load Testing: I performed load testing on my application to replicate the high traffic scenario. This helped me identify that the CORS errors tend to occur during specific peak hours.

My expectation was that after configuring the CORS settings and ensuring proper handling of preflight requests, the CORS errors would not occur, even during high traffic times. I expected the AWS Application Load Balancer and auto scaling group to efficiently distribute traffic and handle the load without causing CORS issues.

However, despite these efforts, I'm still encountering CORS errors during peak traffic times, especially at 9:30 AM. I'm seeking guidance on potential configurations or adjustments I might be overlooking or any best practices for handling CORS in such a setup.

Header settings:

const origin = req.headers.origin;
res.setHeader('Access-Control-Allow-Origin', origin);
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type,accept,authorization');
res.setHeader('Access-Control-Max-Age', '240');
res.setHeader('Access-Control-Allow-Credentials', true);

CORS error

CORS error request

CORS error response

5

1 Answer 1

0

If you have not implemented health checks in your Express.js app and registered them with the load balancer, the freshly started instance can start receiving traffic before it's ready to handle it.

Sign up to request clarification or add additional context in comments.

1 Comment

I have actually tried it without auto scaling too. Without Auto Scaling, when the CPU utilization reaches about 40%, it starts showing CORS error in browser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.