9 months ago
2.0 Flash Experimental. Might not work as expected.
Subject: Persistent 502 Bad Gateway for OPTIONS (Preflight) Requests - Minimal App Test Fails
Hi Railway Support,
I'm encountering a persistent 502 Bad Gateway
error specifically for OPTIONS
(preflight) requests to my backend API. This is causing CORS issues with my frontend, which is hosted on Vercel.
I've taken extensive troubleshooting steps, and I'm confident the problem isn't within my application code. Here's a summary of what I've done:
Correct CORS Configuration: I've implemented CORS middleware in my Express.js backend using the
cors
package. My configuration includes handling preflight requests withapp.options('*', cors(corsOptions))
and uses a dynamic origin to allow both my production frontend URL ([SITE ORIGIN – ASK ME FOR DETAILS IF NEEDED TO TROUBLESHOOT]) and local development (http://localhost:3000
).Verified Middleware Order: I've ensured that the CORS middleware is the very first middleware used in my Express app, before any other middleware or routes.
Created a Minimal Test App: To isolate the problem, I created an extremely simplified Express app that only includes the CORS middleware and a basic test route (
/test-cors
). This minimal app still results in a502
error forOPTIONS
requests. Here's the code for the minimal app:
```const express = require('express');const cors = require('cors');
const app = express();
const port = process.env.PORT || 8080;
const corsOptions = {
origin: '[SITE ORIGIN – ASK ME FOR DETAILS IF NEEDED TO TROUBLESHOOT], // Single origin for this test
};
app.use(cors(corsOptions));
app.options('*', cors(corsOptions));
app.get('/test-cors', (req, res) => {
res.send('CORS test successful!');
});
app.listen(port, "0.0.0.0", () => { // Bound to 0.0.0.0
console.log(`Test server listening on port ${port}`);
});```
Bound to
0.0.0.0
: I've explicitly bound my Express server to0.0.0.0
to ensure it's accessible from outside the container.Checked Application Logs: My application logs are clean and show no errors related to the
502
or CORS.Network Tab Screenshots: I've attached screenshots of my browser's Network tab showing the
502
error for theOPTIONS
request to both my main API endpoint and the/test-cors
route.
Because the 502
error occurs even with the minimal app, I believe the issue is with Railway's infrastructure or configuration. I've also reviewed the documentation and found a similar issue reported by another user (https://help.railway.com/questions/502-bad-gateway-802d0105).
Could you please investigate this issue? I need CORS to work correctly for my application to function.
Thanks in advance for your help!
1 Replies
9 months ago
This thread has been marked as public for community involvement, as it does not contain any sensitive or personal information. Any further activity in this thread will be visible to everyone.
Status changed to Open brody • 9 months ago
9 months ago
Hello,
Not an issue with CORS, OPTIONS, Or Railway's infrastructure.
You had your target port set incorrectly -
https://docs.railway.com/reference/errors/application-failed-to-respond#target-port-set-to-the-incorrect-value
I've gone ahead and fixed that for you, and now I can make an OPTIONS request to your service just fine.