-4
mx := mux.NewRouter()

mx.Use(CorsHandler)


sch := mx.NewRoute().Subrouter()
sch.Use(middleware.ValidateSchoolToken)

teacher := mx.NewRoute().Subrouter()
teacher.Use(middleware.ValidateToken)

CorsHandler is not used when the code runs

jub0bs
  • 60,866
  • 25
  • 183
  • 186
nator
  • 1
  • 2
    Hello @nator welcome to SO, could you describe the error that you are getting? The other thing, that could help your question, is to post a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – Gealber Jul 10 '21 at 02:06
  • Describe the error that you have. And explain your problem – mshomali Jul 10 '21 at 06:37

1 Answers1

1

Have you added http.MethodOptions to your Methods call? I found this sample in the documentation:

func main() {
    r := mux.NewRouter()

    // IMPORTANT: you must specify an OPTIONS method matcher for the middleware to set CORS headers
    r.HandleFunc("/foo", fooHandler).Methods(http.MethodGet, http.MethodPut, http.MethodPatch, http.MethodOptions)
    r.Use(mux.CORSMethodMiddleware(r))
    
    http.ListenAndServe(":8080", r)
}

Full docs: https://pkg.go.dev/github.com/gorilla/mux#readme-handling-cors-requests

AbsentBird
  • 321
  • 1
  • 8