Express app CORS 502 Bad Gateway when deploying to

My Express API runs fine on localhost.
But when I deploy (with Autoscale) to my endpoints fail with 502 Bad Gateway:

Access to fetch at '' from origin '' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Link to my repl:

It works when I make a GET call from Postman to:
but NOT when I call:

I have tried several ideas from other posts, none have worked so far.

Try adding a CORS response header to the response.


I added res.header("Access-Control-Allow-Origin", allowedOrigins) to each endpoint, where allowedOrigins = '*' and got the following behavior:

The first API call to /health-check (from Postman) was 200 OK. All following calls resulted in 502 Bad Gateway.

Can you recommend something else?

Edit: I wrapped my /four-noble-truths endpoint in a try-catch block and I see it is going to the catch block… So, is OpenAI allowed on Replit?

I don’t think Replit explicitly bans it, what error is being thrown to go to the catch?

Got it working! I logged the error message from the catch block - which indicated I had run out of GPT4 credits - and switched the model back to gpt-3.5-turbo and now my app works. Thanks!

1 Like