dqd3690 2015-07-07 08:40
浏览 7

如何设置go服务器使其与AngularJS html5mode兼容?

I am using the julienschmidt/httprouter alongside with http.FileServer.

Something like this:

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))
}

So if a route is not found just serve them from root dir "/"

But this does not work as intended. In nginx I do it like this

location / {
        autoindex on;
        try_files $uri $uri/ /index.html =404;
}

Check if there are any files in the given url, if not then give them /index.html

How can I make the server compatible with AngularJS html5mode?

  • 写回答

1条回答 默认 最新

  • duandi8852752 2015-07-07 10:34
    关注

    If route is not found, add

    router.GET("/", Yourfunction)
    

    and your function as,

    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)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度