Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Web Golang路线不起作用_Web_Go - Fatal编程技术网

Web Golang路线不起作用

Web Golang路线不起作用,web,go,Web,Go,我刚开始进入golang,由于我计划至少托管两个网站,我选择使用Mux通过“过滤”域来显示不同的路由。每当我试图访问我的主要路线,它只会给我一个404错误。(另外,“www”部分不存在的事实是完全正常的。我不会键入它来访问该站点) 但是如果我以文件服务器的形式启动服务器,我就可以访问我的文件,所以我想服务器本身就是在工作的 func redirect(w http.ResponseWriter, req *http.Request) { target := "https://" + re

我刚开始进入golang,由于我计划至少托管两个网站,我选择使用Mux通过“过滤”域来显示不同的路由。每当我试图访问我的主要路线,它只会给我一个404错误。(另外,“www”部分不存在的事实是完全正常的。我不会键入它来访问该站点)

但是如果我以文件服务器的形式启动服务器,我就可以访问我的文件,所以我想服务器本身就是在工作的

func redirect(w http.ResponseWriter, req *http.Request) {
    target := "https://" + req.Host + req.URL.Path
    http.Redirect(w, req, target,
        http.StatusTemporaryRedirect)
}

func main() {
    go http.ListenAndServe(":80", http.HandlerFunc(redirect)) // Redirection

    // Serveur sécurisé

    r := mux.NewRouter()
    r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("/root/go/src/web/static/"))))
    s := r.Host("echecderi.me").Subrouter()
    s.HandleFunc("/", indexEchec)

    http.ListenAndServeTLS(":443", "domain-crt.pem", "domain-key.pem", nil)
}

func indexEchec(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "<h1>Echec de rime</h1> </br> <img src=\"/static/echecderime/echec.gif\">")
}
func重定向(w http.ResponseWriter,req*http.Request){
目标:=“https://”+req.Host+req.URL.Path
http.重定向(w、req、目标、,
http.StatusTemporaryRedirect)
}
func main(){
转到http.ListenAndServe(“:80”,http.HandlerFunc(重定向))//重定向
//sécurisé服务员
r:=mux.NewRouter()
r、 PathPrefix(“/static/”)处理程序(http.StripPrefix(“/static/”),http.FileServer(http.Dir(“/root/go/src/web/static/”)))
s:=r.Host(“echecderi.me”).Subrouter()
s、 扶手板(“/”,索引)
http.listendservetls(“:443”,“domain crt.pem”,“domain key.pem”,nil)
}
func indexEchec(带http.ResponseWriter,r*http.Request){
fmt.Fprintf(w,“犯罪电子计算机”
) }
我认为您需要将
r
作为
http.listend和servetls
的最后一个参数,您还可以使用http.server实例

//create server instance
server := http.Server{
    Addr:      ":443",
    TLSConfig: tlsConfig(cert),
}

rtr := mux.NewRouter()
rtr.HandleFunc("/profile", HandlerProfile).Methods("GET")
//rtr.HandleFunc( other routes...

//pass mux handler to server
server.Handler = rtr
server.ListenAndServeTLS("", "")

我的上帝,谢谢你。在golang文档中,他们使用了
nil
作为示例,因此我认为我需要使用它。但是
r
到底意味着什么呢?最后一个参数是处理程序,它为请求提供服务
r
是一种路由器,它接收请求并根据您设置请求的方式确定如何处理请求。如果将nil处理程序传递给
ListenAndServeTLS
,它将使用默认处理程序(
DefaultServeMux
)。有关更多信息,请参阅。