For development only, you can disable CORS checks:
# Chrome (use for development only!) chrome --user-data-dir="/tmp/chrome_dev" --disable-web-security --disable-features=VizDisplayCompositor # Or use a CORS proxy https://cors-anywhere.herokuapp.com/your-api-url
const cors = require('cors'); const app = express(); // Allow all origins (development only) app.use(cors()); // Or configure specific origins app.use(cors({ origin: ['http://localhost:3000', 'http://localhost:4200'], credentials: true }));
app.use((req, res, next) => { res.header('Access-Control-Allow-Origin', 'http://localhost:3000'); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization'); res.header('Access-Control-Allow-Credentials', true); if (req.method === 'OPTIONS') { res.sendStatus(200); } else { next(); } });
@RestController @CrossOrigin(origins = "http://localhost:3000") public class ApiController { @GetMapping("/api/data") public ResponseEntity<Data> getData() { // your code } }
@Configuration @EnableWebMvc public class WebConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/api/**") .allowedOrigins("http://localhost:3000", "http://localhost:4200") .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") .allowCredentials(true); } }
# Install: pip install django-cors-headers # settings.py INSTALLED_APPS = [ 'corsheaders', # ... other apps ] MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', # ... other middleware ] # Allow specific origins CORS_ALLOWED_ORIGINS = [ "http://localhost:3000", "http://localhost:4200", ] # Or allow all origins (development only) CORS_ALLOW_ALL_ORIGINS = True
# Install: pip install flask-cors from flask import Flask from flask_cors import CORS app = Flask(__name__) # Allow all origins CORS(app) # Or configure specific origins CORS(app, origins=['http://localhost:3000', 'http://localhost:4200']) # Or configure per route from flask_cors import cross_origin @app.route('/api/data') @cross_origin(origins=['http://localhost:3000']) def get_data(): return {'data': 'example'}
public void ConfigureServices(IServiceCollection services) { services.AddCors(options => { options.AddPolicy("AllowSpecificOrigin", builder => { builder.WithOrigins("http://localhost:3000", "http://localhost:4200") .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials(); }); }); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseCors("AllowSpecificOrigin"); // ... other middleware }
{ "name": "my-app", "version": "0.1.0", "proxy": "http://localhost:8080", // ... rest of package.json }
// Create setupProxy.js in src folder const { createProxyMiddleware } = require('http-proxy-middleware'); module.exports = function(app) { app.use( '/api', createProxyMiddleware({ target: 'http://localhost:8080', changeOrigin: true, }) ); };
{ "/api/*": { "target": "http://localhost:8080", "secure": true, "changeOrigin": true, "logLevel": "debug" } }
ng serve --proxy-config proxy.conf.json
module.exports = { devServer: { proxy: { '/api': { target: 'http://localhost:8080', changeOrigin: true, pathRewrite: { '^/api': '' } } } } }
Frontend on localhost:3000 calling API on localhost:8080
Access-Control-Allow-Origin: http://localhost:3000
header to API responseSending POST with Content-Type: application/json
Access-Control-Allow-Methods: POST
Access-Control-Allow-Headers: Content-Type
Sending cookies or authorization headers
Access-Control-Allow-Credentials: true