Hi Everybody,
There are several ways to implement CORS on go server but I found one of the easiest way to enable CORS (Cross Origin Resource Sharing) on the Go server
- which helps the API to get integrate with the front-end UI easily without getting CORS issue.
package main
import (
"net/http"
"github.com/rs/cors"
)
func main() {
serve_mux := http.NewServeMux()
serve_mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("{\"Hello\": \"from CORS enabled server\"}"))
})
handler := cors.Default().Handler(serve_mux)
http.ListenAndServe(":80", handler)
}
Note : Please do go get github.com/rs/cors since this program uses rs/cors (external library)
- which helps the API to get integrate with the front-end UI easily without getting CORS issue.
package main
import (
"net/http"
"github.com/rs/cors"
)
func main() {
serve_mux := http.NewServeMux()
serve_mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("{\"Hello\": \"from CORS enabled server\"}"))
})
handler := cors.Default().Handler(serve_mux)
http.ListenAndServe(":80", handler)
}
Note : Please do go get github.com/rs/cors since this program uses rs/cors (external library)
No comments:
Post a Comment