I have an angular application which is querying a node/express backend endpoint every 5 seconds. However, occasionally i am getting a cors error.
Usecase
- A user creates a transaction. The transaction gets added to the database with the status of "Pending"
- The frontend periodically queries the database until the status has been updated to "Completed". So it can retrieve the results.
Angular
Every 5 seconds i am doing a http get request
const url = 'https://<removed>.com/api/v1/transactions/' + transactionId;
interval(5000)
.pipe(
switchMap(() => this.httpClient.get(url)),
tap((response: any) => {
this.summary = response;
}),
takeWhile((response: any) => response.data['status'] !== 'Completed'),
).subscribe();
}
The first request is fine, then i get the following error on the next request.
Failed to load https://"".com/api/v1/transactions/f3debad2-a830-4168-9a03-475389dae7e0: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 502.
Cross-Origin Read Blocking (CORB) blocked cross-origin response https://"".com/api/v1/transactions/f3debad2-a830-4168-9a03-475389dae7e0 with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.
Again this only happens sometimes, other times i can make as many requests i want with 200 responses.
Node
I set CORS in my backend.
app.use(cors());
Note
I'm not sure if its a complete server-side issue. This is a random edge case. All my other API requests including POST requests work fine.
Infact before it starts the interval of 5 seconds to check if the results are ready, it posts to endpoint on the same domain, with no problems.
I have done 10 test cases, each test will query the endpoint 3-4 times (usuaully takes this long before the results are ready/complete). All 10 test cases could be fine then the 11th might have this problem.