As asked
Your React app on app.example.com fetches from api.example.com. Explain what CORS is, when a preflight OPTIONS request is triggered versus not triggered, and what the server must include in its response headers to allow the request.
Sample answer outline
CORS is the browser policy restricting cross-origin requests. Simple requests (GET/POST with basic content types) are sent directly; the browser checks the response Access-Control-Allow-Origin. Preflighted requests occur for methods like PUT/DELETE or custom headers; the browser sends OPTIONS first. Server must respond with ACAO, ACAM, and ACAH headers. Should mention that CORS is enforced by the browser, not the server.
Expect these follow-ups
- What is the wildcard in Access-Control-Allow-Origin and when can you not use it?
- How would you configure CORS in a Next.js API route or middleware?