duanjianlu0506 2017-04-21 17:51
浏览 39
已采纳

使用大猩猩/ mux golang库时出现404错误

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条回答 默认 最新

  • dswe30290 2017-04-21 17:54
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?