英文:
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/
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论