我遇到了此代码,并希望在端口1111、1112和1113上允许CORS,该怎么办?
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/all", All).Methods("GET")
router.HandleFunc("/gotoT", Goto).Methods("GET")
headers := handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"})
methods := handlers.AllowedMethods([]string{"GET", "POST", "PUT", "HEAD", "OPTIONS"})
origins := handlers.AllowedOrigins([]string{"*"})
log.Fatal(http.ListenAndServe(":1111", handlers.CORS(headers, methods, origins)(router)))
在此示例中, CORS仅在端口1111 和上监听,因此我想添加更多端口,请帮忙吗?
最佳答案
将它们放在单独的go例程中:
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/all", All).Methods("GET")
router.HandleFunc("/gotoT", Goto).Methods("GET")
headers := handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"})
methods := handlers.AllowedMethods([]string{"GET", "POST", "PUT", "HEAD", "OPTIONS"})
origins := handlers.AllowedOrigins([]string{"*"})
corsRouter := handlers.CORS(headers, methods, origins)(router)
go func() {
log.Fatal(http.ListenAndServe(":1111", corsRouter))
}()
go func() {
log.Fatal(http.ListenAndServe(":1112", corsRouter))
}()
log.Fatal(http.ListenAndServe(":1113", corsRouter))
关于go - 如何允许CORS使用多个端口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64085188/