doumen5895 2014-10-17 18:32
浏览 67
已采纳

Go + Angular:加载基本HTML

I'm trying to write an app in Go with Angular. I'm not sure if I got the concept right, but basically I should serve a simple html that loads angular and the app (js) itself and then the rest is handled by ajax requests. What I don't know is how to serve the html file on every non-ajax request on every path? I would like to use Gorilla mux but I couldn't find out how to do that.

Is this even the right direction?

  • 写回答

2条回答 默认 最新

  • dongye7231 2014-10-17 18:42
    关注

    On every request that is not any known url You should send index.html - or whatever is Your base angular app file.

    Gorilla/mux has a NotFoundHandler, which is handler for everyting that is not matched by any other routes. You can assignt Your own handler for it like that:

    solution with gorilla/mux is:

    func main() {
        r := mux.NewRouter()
        r.HandleFunc("/foo", fooHandler)
        r.NotFoundHandler = http.HandlerFunc(notFound)
        http.Handle("/", r)
    
    }
    

    while notFound is:

    func notFound(w http.ResponseWriter, r *http.Request) {
        http.ServeFile(w, r, "static/index.html")
    }
    

    assuming the Your base file is in static/index.html :).

    Now all Your requests that are not any other requests (so, in that setup - not an ajax call defined in routes) will serve index file with url that can be handled by ngRoute or ui-router.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型