我将julienschmidt/httproouter与http.FileServer一起使用。
像这样:
func main() {
router := httprouter.New()
router.NotFound = http.FileServer(http.Dir("/srv/www/public_html"))
router.GET("/api/user/:id", getUserbyId)
log.Fatal(http.ListenAndServe(":80", router))
}
所以如果没有找到路由,就从根目录"/"
但是这并没有像预期的那样工作。在nginx中,我是这样做的
location / {
autoindex on;
try_files $uri $uri/ /index.html =404;
}
检查给定url中是否有文件,如果没有则给它们/index.html
如何使服务器兼容AngularJS的html5mode?
如果没有找到路由,添加
router.GET("/", Yourfunction)
和你的功能,
func Yourfunction(res http.ResponseWriter, req *http.Request) {
file, _ := ioutil.ReadFile("/srv/www/public_html")
t := template.New("")
t, _ = t.Parse(string(file))
t.Execute(res, nil)
}