What is CORS?
Cross-Origin Resource Sharing (CORS) is a security feature implemented by web browsers that restricts web pages from making requests to a different domain than the one serving the web page. This is known as the “same-origin policy.”CORS is essential when your frontend application (e.g., running on
http://localhost:5173) needs to communicate with your API (running on http://localhost:3000).Why CORS Matters
- Browser Security: Prevents malicious websites from accessing your API
- Controlled Access: Allows you to specify which origins can access your resources
- Production Safety: Protects your API from unauthorized cross-origin requests
Default CORS Configuration
API Master comes with a pre-configured CORS setup. Here’s the actual implementation fromapp.ts:
app.ts
Configuration Breakdown
origin: '*'
origin: '*'
What it does: Allows requests from any originUse case: Perfect for development and testingSecurity: ⚠️ Not recommended for productionThe wildcard
'*' means any website can make requests to your API. This is convenient during development but poses security risks in production.methods: ['GET', 'POST', 'PUT', 'DELETE']
methods: ['GET', 'POST', 'PUT', 'DELETE']
What it does: Specifies which HTTP methods are allowed for cross-origin requestsAllowed operations:
GET- Retrieve dataPOST- Create resources (e.g., file upload)PUT- Update resourcesDELETE- Remove resources
PATCH), the browser will block the request.allowedHeaders: ['Content-Type', 'Authorization']
allowedHeaders: ['Content-Type', 'Authorization']
Production Configuration
For production environments, you should restrict access to specific domains. Here’s how to configure secure CORS:- Single Origin
- Multiple Origins
- Environment-Based
app.ts
https://yourdomain.com will be allowed.Credentials Support
If your API needs to work with cookies, authentication tokens, or HTTP authentication:Development vs Production Setup
Advanced Configuration
Additional Headers
If you need to support custom headers:Preflighted Requests
For complex requests (e.g., with custom headers), browsers send a preflight OPTIONS request:Troubleshooting CORS Issues
Common Errors and Solutions
Error: No 'Access-Control-Allow-Origin' header
Error: No 'Access-Control-Allow-Origin' header
Problem: CORS middleware not applied or origin not allowedSolutions:
- Verify
app.use(cors(corsOptions))is before your routes - Check if your origin is in the allowed list
- Ensure the API server is running
Error: CORS policy blocks credentials
Error: CORS policy blocks credentials
Problem: Using
origin: '*' with credentials: trueSolution: Specify exact origin(s):Error: Method not allowed by CORS
Error: Method not allowed by CORS
Problem: HTTP method not in allowed methods listSolution: Add the method to your configuration:
Error: Header is not allowed by CORS
Error: Header is not allowed by CORS
Problem: Client sending headers not in
allowedHeadersSolution: Add required headers:Testing CORS Configuration
Test your CORS setup with cURL:Best Practices
Use environment variables
Store allowed origins in environment variables for easy configuration across environments.
Enable credentials only when needed
Only set
credentials: true if you’re using cookies or HTTP authentication.CORS is a browser security feature. Tools like Postman, cURL, and server-to-server requests bypass CORS entirely.