Go + Angular ui-router(路由器)

huangapple go评论125阅读模式
英文:

Go + Angular ui-router

问题

我是一个新的Gopher,正在尝试使用Go后端为我的AngularJS前端提供服务,并提供API。

这是我目前的代码:

  1. package main
  2. import (
  3. "github.com/gorilla/mux"
  4. "log"
  5. "net/http"
  6. )
  7. func main() {
  8. rtr := mux.NewRouter()
  9. srtr := rtr.PathPrefix("/api").Subrouter()
  10. srtr.HandleFunc("/hello", hello).Methods("GET")
  11. rtr.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/")))
  12. http.Handle("/", rtr)
  13. log.Println("Listening...")
  14. http.ListenAndServe(":3000", nil)
  15. }
  16. func hello(w http.ResponseWriter, r *http.Request) {
  17. w.Write([]byte("Hello World"))
  18. }

一切正常。/api/hello 返回 "Hello World",如果我访问 /,它将提供我的 index.html。然而,由于我正在尝试使用Angular UI Router,所以我希望我的Go服务器将所有未注册的路由发送到Angular,以便Angular UI Router可以处理它们。

例如:如果我现在访问 /random,它将返回404,因为在 ./static 目录下没有名为 random 的文件。但是我希望Go将该请求转发给Angular,以便ui-router可以处理 /random

英文:

I'm a new Gopher trying to do a Go backend to serve my Angularjs frontend and also serve an API.

This is what I have so far.

  1. package main
  2. import (
  3. "github.com/gorilla/mux"
  4. "log"
  5. "net/http"
  6. )
  7. func main() {
  8. rtr := mux.NewRouter()
  9. srtr := rtr.PathPrefix("/api").Subrouter()
  10. srtr.HandleFunc("/hello", hello).Methods("GET")
  11. rtr.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/")))
  12. http.Handle("/", rtr)
  13. log.Println("Listening...")
  14. http.ListenAndServe(":3000", nil)
  15. }
  16. func hello(w http.ResponseWriter, r *http.Request) {
  17. w.Write([]byte("Hello World"))
  18. }

Everything works fine. /api/hello return "Hello World" and if I go to / it will serve my index.html. However since I'm trying to use angular ui-router so I need my go server to send all non-registered routes to angular so angular ui-router can handle them.

For example: If I go /random right now it will return a 404 since I don't have any file under ./static named random. But what I want is Go to forward that request to Angular so ui-router can handle the /random

答案1

得分: 3

在你的路由器中,你应该将index.html提供给所有未定义的其他URL。在mux包中,有一个有用的处理程序:http://www.gorillatoolkit.org/pkg/mux#Router - 查看NotFoundHandler

你可以使用它来处理所有的404错误,并代替提供index.html:

  1. func main() {
  2. r := mux.NewRouter()
  3. r.HandleFunc("/foo", fooHandler)
  4. r.NotFoundHandler = http.HandlerFunc(notFound)
  5. http.Handle("/", r)
  6. }
  7. func notFound(w http.ResponseWriter, r *http.Request) {
  8. http.ServeFile(w, r, "static/index.html")
  9. }
英文:

In Your router You should serve index.html to all undefined elsewhere URLs. In mux package there is helpful handler:
http://www.gorillatoolkit.org/pkg/mux#Router

  • look at NotFoundHandler

You can use it, to handle all 404's and serve index.html instead:

  1. func main() {
  2. r := mux.NewRouter()
  3. r.HandleFunc("/foo", fooHandler)
  4. r.NotFoundHandler = http.HandlerFunc(notFound)
  5. http.Handle("/", r)
  6. }

and define notFound function:

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

huangapple
  • 本文由 发表于 2014年7月21日 06:07:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/24855388.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定