如何允许 CORS 使用多个端口?
How can I allow CORS using many ports?
我看到这段代码,想在端口 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))
我看到这段代码,想在端口 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))