What is CORS and how do you enable it in Express?

Answer

CORS (Cross-Origin Resource Sharing) is a browser security mechanism that blocks web pages from making requests to a different domain than the one that served the page. When your React frontend on localhost:3000 calls your Express API on localhost:5000, the browser blocks it unless the server includes the appropriate CORS headers. In Express, install and use the cors package: const cors = require('cors'); app.use(cors()) — this allows all origins. For production, configure specific origins: app.use(cors({ origin: 'https://myapp.com' })). You can also allow specific methods, headers, and credentials. CORS headers only apply to browser clients — tools like Postman and curl are unaffected.