Angular 戈朗+;有棱角的

Angular 戈朗+;有棱角的,angular,go,routing,Angular,Go,Routing,我开始尝试使用Go和Angular,但我有一个奇怪的问题。。我想我只是遗漏了一个小细节,但我想不出来 我用的是围棋的路由器。。。现在使用Angular,我应该能够将URL复制并粘贴到浏览器中,Angular应该能够处理相应的路由,对吗 我有一个“/登录”路径。如果通过前端访问路由,则此功能将起作用。。。。但如果我在浏览器中输入“mypage.com/login”,就会得到404 走路线基本上就是做 router.NotFound = http.FileServer(http.Dir("./pub

我开始尝试使用Go和Angular,但我有一个奇怪的问题。。我想我只是遗漏了一个小细节,但我想不出来

我用的是围棋的路由器。。。现在使用Angular,我应该能够将URL复制并粘贴到浏览器中,Angular应该能够处理相应的路由,对吗

我有一个“/登录”路径。如果通过前端访问路由,则此功能将起作用。。。。但如果我在浏览器中输入“mypage.com/login”,就会得到404

走路线基本上就是做

router.NotFound = http.FileServer(http.Dir("./public"))

它适用于“/”路由,但不适用于任何其他路由。这似乎是正确的。但是如何正确设置路由,以便Angular处理所有路由?

您可以直接使用
http

索引页 这将为所有与路由不匹配的请求提供index.html文件

文件服务器 这将为公共目录中的所有文件提供服务

别忘了启动你的服务器

http.ListenAndServe(":8000", nil)

使用枸杞微框架

它很容易使用

func render_html_page(w http.ResponseWriter, url string) {
    t, err := template.ParseFiles(url) 
    if err != nil {
        panic (err)
    }
    t.Execute(w, nil)
}

func index(c web.C, w http.ResponseWriter, r *http.Request) {
    render_html_page(w, "./public/index.html")
}

func main() {
        goji.Get("/", index)
        goji.Serve()
}

这段代码可以工作,您只需进行导入

我就遇到了404问题。此路由是HTML5模式。你需要告诉app.yaml中的处理程序。检查我的英雄之旅叉子项目在这里

将此添加到app.yaml可能会解决此问题

- url: /.*
  static_files: index.html
  upload: index.html

这就是我在标准Go库中使用的,路由非常有效

查看

//创建一个新的服务多路复用器
mux:=http.NewServeMux()
//为静态文件服务创建空间
mulx.Handle(“/node\u modules/”,http.StripPrefix(“/node\u modules”),http.FileServer(http.Dir(“/node\u modules”))
mulx.Handle(“/html/”,http.StripPrefix(“/html”,http.FileServer(http.Dir(“/html”))
mulx.Handle(“/js/”,http.StripPrefix(“/js”,http.FileServer(http.Dir(“/js”)))
mulx.Handle(“/ts/”,http.StripPrefix(“/ts”,http.FileServer(http.Dir(“/ts”)))
mux.Handle(“/css/”,http.StripPrefix(“/css”,http.FileServer(http.Dir(“/css”)))
//做你的api工作**
mulx.Handle(“/api/register”),util.Adapt(api.RegisterHandler(mux),
api.GetMongoConnection(),
api.CheckEmptyUserForm(),
api.EncodeUserJson(),
api.ExpectBody(),
api.ExpectPOST(),
))
mux.HandleFunc(“/api/login”,api.login)
mux.HandleFunc(“/api/authenticate”,api.authenticate)
//任何其他请求,我们都应该呈现SPA唯一的html文件,
//允许angular在api之外的任何其他对象上进行路由
//以及它自身工作所需的文件。
//这里的秩序至关重要。此html应包含基本标记,如
//*此处href应与下面的HandleFunc路径匹配
mux.HandleFunc(“/”,func(w http.ResponseWriter,r*http.Request){
http.ServeFile(w,r,“html/index.html”)
})

请定义router.Notfound处理程序以提供angular index.html文件

import (
  "log"
  "net/http"

  "github.com/julienschmidt/httprouter"
)

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

func main() {
  router := httprouter.New()

  // handle angular
  router.NotFound = http.HandlerFunc(angularHandler)

  // serve static files
  router.ServeFiles("/*filepath", http.Dir("./public"))

  log.Fatal(http.ListenAndServe(":3000", router))
}

同样的问题,我访问“/登录”并获得404。你还在使用httprouter吗?不,我没有。去吧。使用您编辑的解决方案,我得到了空白页,所以我想这已经是一步了。是的,我得到了“意外标记是的,我也得到了”解释为样式表但使用MIME类型文本/html传输的资源“…为什么这被否决了?我得到了相同的问题!!
- url: /.*
  static_files: index.html
  upload: index.html
// Creates a new serve mux
mux := http.NewServeMux()

// Create room for static files serving
mux.Handle("/node_modules/", http.StripPrefix("/node_modules", http.FileServer(http.Dir("./node_modules"))))
mux.Handle("/html/", http.StripPrefix("/html", http.FileServer(http.Dir("./html"))))
mux.Handle("/js/", http.StripPrefix("/js", http.FileServer(http.Dir("./js"))))
mux.Handle("/ts/", http.StripPrefix("/ts", http.FileServer(http.Dir("./ts"))))
mux.Handle("/css/", http.StripPrefix("/css", http.FileServer(http.Dir("./css"))))

// Do your api stuff**
mux.Handle("/api/register", util.Adapt(api.RegisterHandler(mux),
    api.GetMongoConnection(),
    api.CheckEmptyUserForm(),
    api.EncodeUserJson(),
    api.ExpectBody(),
    api.ExpectPOST(),

))
mux.HandleFunc("/api/login", api.Login)
mux.HandleFunc("/api/authenticate", api.Authenticate)

// Any other request, we should render our SPA's only html file,
// Allowing angular to do the routing on anything else other then the api    
// and the files it needs for itself to work.
// Order here is critical. This html should contain the base tag like
// <base href="/"> *href here should match the HandleFunc path below 
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, "html/index.html")
})
import (
  "log"
  "net/http"

  "github.com/julienschmidt/httprouter"
)

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

func main() {
  router := httprouter.New()

  // handle angular
  router.NotFound = http.HandlerFunc(angularHandler)

  // serve static files
  router.ServeFiles("/*filepath", http.Dir("./public"))

  log.Fatal(http.ListenAndServe(":3000", router))
}