Should you use a proxy or configure CORS in development?
Use a proxy in development (simpler, avoids CORS entirely). Configure CORS on the server for production. The proxy makes the browser think the request is same-origin, so no CORS headers are needed.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in CORS: Real-World Examples
Use the cors middleware: app.use(cors({ origin: ['https://myapp.com'], methods: ['GET', 'POST', 'PUT', 'DELETE'], credentials: true })). Or set headers manually and return 204 for OPTIONS.
Set headers in the handler: res.setHeader('Access-Control-Allow-Origin', 'https://myapp.com'); res.setHeader('Access-Control-Allow-Methods', 'GET, POST'); if (req.method === 'OPTIONS') return res.status(204).end();
In vite.config.js: server: { proxy: { '/api': { target: 'https://api.example.com', changeOrigin: true } } }. Requests to /api are proxied to the API, avoiding CORS (same-origin from the browser's perspective).
Still have questions?
Browse all our FAQs or reach out to our support team
