为了账号安全,请及时绑定邮箱和手机立即绑定

如何允许 CORS 使用多个端口?

如何允许 CORS 使用多个端口?

Go
炎炎设计 2022-07-04 16:15:30
我遇到了这段代码,想在端口 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,所以我想添加更多端口,请帮忙?
查看完整描述

1 回答

?
MMTTMM

TA贡献1869条经验 获得超4个赞

将它们放在单独的 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))


查看完整回答
反对 回复 2022-07-04
  • 1 回答
  • 0 关注
  • 154 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号