R
Railwayβ€’14mo ago
Mason

Suddenly getting CORS errors sending requests to my backend express server

Hi, I've configured an accepted origin for requests in each of my environments as follows:
app.use(express.json())
app.use(
cors({
origin: process.env.CORS_ALLOWED_ORIGIN,
preflightContinue: true,
})
)
app.use(express.json())
app.use(
cors({
origin: process.env.CORS_ALLOWED_ORIGIN,
preflightContinue: true,
})
)
As you can see the allowed origin for this environment is stored in the variables, e.g. https://myapp-frontend-dev.up.railway.app/ I've double checked this is loaded fine in the backend, and before today wasn't having any issues with this, but now all requests to my backend from the frontend service fail with:
Access to fetch at 'https://myapp-backend-dev.up.railway.app/stripe/getUsage' from origin 'https://myapp-frontend-dev.up.railway.app' has been blocked by CORS policy: 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.
Access to fetch at 'https://myapp-backend-dev.up.railway.app/stripe/getUsage' from origin 'https://myapp-frontend-dev.up.railway.app' has been blocked by CORS policy: 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.
My frontend can no longer interact with the backend at all, including in environments where the code hasn't been touched in days. Anyone aware what could be the issue?
9 Replies
Percy
Percyβ€’14mo ago
Project ID: 2818bb01-9898-4d1c-8065-06e6404b3ed8
Mason
MasonOPβ€’14mo ago
Project ID: 2818bb01-9898-4d1c-8065-06e6404b3ed8 Oddly things still seem to be fine in my prod environment, which is configured the same way. Seems to just be certain endpoints of the backend service which are affected. πŸ€” Locally the same code is working fine, double checked the variables I'm using, that when I'm removing them I also get CORS issues on localhost, etc.
Brody
Brodyβ€’14mo ago
can you provide a link that would cause a cors error? not sure something that is called getSubscriptions should be a POST, but okay i will try
Mason
MasonOPβ€’14mo ago
Agree
Brody
Brodyβ€’14mo ago
okay i see you are setting a access-control-allow-origin so thats good, can you give me a link to the frontend where i can see a cors error
Mason
MasonOPβ€’14mo ago
Resolved, missing variables in the services
Brody
Brodyβ€’14mo ago
oh awsome, all good then?
Mason
MasonOPβ€’14mo ago
Except that it seems like an extremely weird error to get for a missing env var while an endpoint processes a request, buuuuut I will look into it. Thanks again for your attention.
Brody
Brodyβ€’14mo ago
no problem! πŸ™‚
Want results from more Discord servers?
Add your server