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

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

404 Error while using gorilla/mux golang library

问题

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

  1. package main
  2. import (
  3. "log"
  4. "github.com/gorilla/mux"
  5. "net/http"
  6. "time"
  7. )
  8. func main() {
  9. r := mux.NewRouter()
  10. r.PathPrefix("/Login/").Handler(http.StripPrefix("/Login/",
  11. http.FileServer(http.Dir("$HOME/Documents/TEST/Login"))))
  12. srv := &http.Server{
  13. Handler: r,
  14. Addr: "{address}:9999",
  15. WriteTimeout: 600 * time.Second,
  16. ReadTimeout: 600 * time.Second,
  17. }
  18. log.Fatal(srv.ListenAndServe())
  19. }
英文:

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.

  1. package main
  2. import (
  3. "log"
  4. "github.com/gorilla/mux"
  5. "net/http"
  6. "time"
  7. )
  8. func main() {
  9. r := mux.NewRouter()
  10. r.PathPrefix("/Login/").Handler(http.StripPrefix("/Login/",
  11. http.FileServer(http.Dir("$HOME/Documents/TEST/Login"))))
  12. srv := &http.Server{
  13. Handler: r,
  14. Addr: "{address}:9999",
  15. WriteTimeout: 600 * time.Second,
  16. ReadTimeout: 600 * time.Second,
  17. }
  18. log.Fatal(srv.ListenAndServe())
  19. }

答案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:

确定