英文:
Go Mux Middlware not using my CORS handler
问题
mx := mux.NewRouter()
mx.Use(CorsHandler)
sch := mx.NewRoute().Subrouter()
sch.Use(middleware.ValidateSchoolToken)
teacher := mx.NewRoute().Subrouter()
teacher.Use(middleware.ValidateToken)
当代码运行时,不使用CorsHandler。
英文:
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
答案1
得分: 1
你是否在Methods调用中添加了http.MethodOptions
?我在文档中找到了这个示例:
func main() {
r := mux.NewRouter()
// 重要提示:你必须为中间件指定一个OPTIONS方法匹配器以设置CORS头部
r.HandleFunc("/foo", fooHandler).Methods(http.MethodGet, http.MethodPut, http.MethodPatch, http.MethodOptions)
r.Use(mux.CORSMethodMiddleware(r))
http.ListenAndServe(":8080", r)
}
完整文档:https://pkg.go.dev/github.com/gorilla/mux#readme-handling-cors-requests
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论