Go + Angular ui-router(路由器)

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

Go + Angular ui-router

问题

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

这是我目前的代码:

package main

import (
    "github.com/gorilla/mux"
    "log"
    "net/http"
)

func main() {
    rtr := mux.NewRouter()
    srtr := rtr.PathPrefix("/api").Subrouter()
    srtr.HandleFunc("/hello", hello).Methods("GET")
    rtr.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/")))

    http.Handle("/", rtr)

    log.Println("Listening...")
    http.ListenAndServe(":3000", nil)
}

func hello(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello World"))
}

一切正常。/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.

package main

import (
    "github.com/gorilla/mux"
    "log"
    "net/http"
)

func main() {
    rtr := mux.NewRouter()
    srtr := rtr.PathPrefix("/api").Subrouter()
    srtr.HandleFunc("/hello", hello).Methods("GET")
    rtr.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/")))

    http.Handle("/", rtr)

    log.Println("Listening...")
    http.ListenAndServe(":3000", nil)
}

func hello(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello World"))
}

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:

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

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

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:

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

and define notFound function:

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

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:

确定