7 months ago
login:1 Access to XMLHttpRequest at 'https://parkynode-production.up.railway.app/api/auth/login' from origin 'https://parky.in' 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.
code:
// ✅ Define allowed origins
const allowedOrigins = [
'http://localhost:5173',
'https://parky.in',
'https://www.parky.in'
];
// ✅ Configure CORS with credentials
app.use(cors({
origin: function (origin, callback) {
if (!origin || allowedOrigins.includes(origin)) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization']
}));4 Replies
7 months ago
Hey there! We've found the following might help you get unblocked faster:
If you find the answer from one of these, please let us know by solving the thread!
7 months ago
Solution
Ensure you place the app.use(cors(...)) call before you define any routes in your Express application. The CORS middleware needs to handle the preflight OPTIONS request before it reaches your route handlers.
const express = require('express');
const cors = require('cors');
const app = express();
// --- CORS Configuration ---
// THIS MUST COME BEFORE YOUR ROUTES
const allowedOrigins = [
'http://localhost:5173',
'https://parky.in',
'https://www.parky.in'
];
const corsOptions = {
origin: function (origin, callback) {
// allow requests with no origin (like mobile apps or curl requests)
if (!origin || allowedOrigins.includes(origin)) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
credentials: true,
};
app.use(cors(corsOptions));
// It's also good practice to enable pre-flight requests for all routes
app.options('*', cors(corsOptions));
// --- Other Middleware ---
app.use(express.json());
// etc.
// --- YOUR API ROUTES ---
// e.g., app.use('/api/auth', authRoutes);
// e.g., app.post('/api/auth/login', ...);
app.get('/', (req, res) => {
res.send('API is running...');
});
// ... rest of your server setup
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
7 months ago
Hi, I made the changes as per your suggestion. Code is working fine in local. But getting the same CORS blocked error. I tried ChatGPT and Claude and code seems to be right. Probably issue with Railway side. Please check as a priority, this has been over 3 days.
7 months ago
Can you just try simple
Allow all origins
app.use(cors())
Then deploy to railway and then see if you are getting the error.