Go 有没有办法通过Http.Handle或Http.FileServer传入头文件?
我在Go中设置了一个非常基本的服务器Go 有没有办法通过Http.Handle或Http.FileServer传入头文件?,go,http-headers,Go,Http Headers,我在Go中设置了一个非常基本的服务器 fs:=http.FileServer(http.Dir(“./public”)) http.Handle(“/”,fs) 但问题是:我希望人们使用fetch()访问我的URL。但是,这是不可能的,因为正在设置CORS Access to fetch 'xxxxx' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is p
fs:=http.FileServer(http.Dir(“./public”))
http.Handle(“/”,fs)
但问题是:我希望人们使用fetch()
访问我的URL。但是,这是不可能的,因为正在设置CORS
Access to fetch 'xxxxx' from origin 'null' has
been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested
resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch
the resource with CORS disabled.
我需要找到一种方法来传递头访问控制允许源代码:
,但我不知道如何使用http.Handle
或http.FileServer
,仅http.HandleFunc
我不能使用http.HandleFunc
,因为据我所知,它不允许我提供文件,而且我不希望自己使用文件处理系统获取文件(除非有其他方法,否则我可能不得不这样做)。另外,它的效率很低。为什么要重新发明轮子,尤其是当轮子比我能想到的要好得多的时候
是否有任何方法可以使用http.Handle()
发送头
我是一个新手,我有一段时间没有使用过静态类型语言,也没有使用过处理传入URL的语言(我主要使用PHP,所以…),所以我可能对这个概念有很好的理解,也可能没有很好的理解。你可以将
http.FileServer
包装在http.HandleFunc
中:
func-cors(fs-http.Handler)http.HandlerFunc{
return func(w http.ResponseWriter,r*http.Request){
//做你的cors工作
//如果不希望文件服务器处理特定请求,则返回
财政司司长(西、右)
}
}
然后将其用于:
fs:=http.FileServer(http.Dir(“./public”))
http.Handle(“/”,cors(fs))
底层机制是
http.HandleFunc
实现http.HandleFunc
接口。您可以将http.FileServer
包装在http.HandleFunc
中:
func-cors(fs-http.Handler)http.HandlerFunc{
return func(w http.ResponseWriter,r*http.Request){
//做你的cors工作
//如果不希望文件服务器处理特定请求,则返回
财政司司长(西、右)
}
}
然后将其用于:
fs:=http.FileServer(http.Dir(“./public”))
http.Handle(“/”,cors(fs))
底层机制是
http.HandlerFunc
实现http.Handler
接口。这不仅有助于解决我的问题,还可能有助于我解决未来的许多问题。谢谢你帮我卸下了背上的重担!哦,哇。这不仅有助于解决我的问题,还可能有助于我解决未来的许多问题。谢谢你帮我卸下了背上的重担!