Express app CORS 502 Bad Gateway when deploying to replit.app

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

Access to fetch at 'https://boodi-proxy-alexcris1.replit.app/four-noble-truths' from origin 'https://boodi-ai-alexcris1.replit.app' 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:
https://replit.com/@AlexCris1/Boodi-Proxy

It works when I make a GET call from Postman to:
https://boodi-proxy.alexcris1.repl.co/health-check
but NOT when I call:
https://boodi-proxy-alexcris1.replit.app/health-check

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

Try adding a CORS response header to the response.

2 Likes

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