Back to course

Middleware for CORS

Go (Golang) for Cloud-Native Microservices

Cross-Origin Resource Sharing

If your frontend is on react.com and your API is on api.com, the browser will block requests unless you handle CORS.

go func corsMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r http.Request) { w.Header().Set("Access-Control-Allow-Origin", "") if r.Method == "OPTIONS" { return // Handle preflight } next.ServeHTTP(w, r) }) }