使用gorilla/mux golang库时出现404错误。

huangapple go评论81阅读模式
英文:

404 Error while using gorilla/mux golang library

问题

我对golang还比较新手,最近在编写一个简单的文件服务程序时遇到了一些困惑。我怀疑在处理程序中的路由器r的文件前缀/目录方面出了问题。我尝试了很多不同的目录格式。我想要服务的HTML文件位于我的系统上的$HOME/Documents/TEST/Login。以下是我的代码,注意{address}是替换为IP地址的:

package main

import (
	"log"
	"github.com/gorilla/mux"
	"net/http"
	"time"
)

func main() {
	r := mux.NewRouter()
	r.PathPrefix("/Login/").Handler(http.StripPrefix("/Login/", 
	http.FileServer(http.Dir("$HOME/Documents/TEST/Login"))))

	srv := &http.Server{
		Handler:      r,
		Addr:         "{address}:9999",
		WriteTimeout: 600 * time.Second,
		ReadTimeout:  600 * time.Second,
	}

	log.Fatal(srv.ListenAndServe())
}
英文:

I am fairly new to golang and am finding myself frustrated with a simple file service program. I am suspecting that there is something wrong with my file prefix/ directory in the handler for my router r. I have tried many different formats for the directory. the html file i would like serviced is $HOME/Documents/TEST/Login on my system. Below is my code, note the {address} replaces the ip address.

    package main

    import (
       "log"
       "github.com/gorilla/mux"
       "net/http"
       "time"
     )

   func main() {
   r := mux.NewRouter()
   r.PathPrefix("/Login/").Handler(http.StripPrefix("/Login/", 
   http.FileServer(http.Dir("$HOME/Documents/TEST/Login"))))

   srv := &http.Server{
       Handler:      r,
       Addr:         "{address}:9999",
       WriteTimeout: 600 * time.Second,
       ReadTimeout:  600 * time.Second,
   }

   log.Fatal(srv.ListenAndServe())
   }

答案1

得分: 0

Go不会解释$HOME。请使用明确的路径,例如/home/username/Documents/TEST/Login/

英文:

Go won't interpret $HOME. Use an explicit path such as /home/username/Documents/TEST/Login/.

huangapple
  • 本文由 发表于 2017年4月22日 01:51:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/43549116.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定