英文:
Golang + Angular
问题
我开始尝试使用Go和Angular进行开发,但是我遇到了一个奇怪的问题...我猜我只是漏掉了一个小细节,但是我无法找出问题所在。
我正在使用https://github.com/julienschmidt/httprouter作为Go的路由器...现在使用Angular,我应该能够将URL复制并粘贴到浏览器中,然后Angular应该处理相应的路由,对吗?
我有一个"/login"的路由。如果通过前端访问该路由,它是有效的...但是如果我在浏览器中输入"mypage.com/login",就会得到404错误。
Go的路由基本上只是这样处理的:
router.NotFound = http.FileServer(http.Dir("./public"))
这对于"/"路由有效,但对于其他任何路由都无效。这似乎是正确的。但是我该如何正确设置路由,以便让Angular处理所有的路由呢?
英文:
I started trying to work with Go and Angular, but I have a weird issue.. I guess I'm just missing a tiny detail, but I can't figure it out.
I'm using https://github.com/julienschmidt/httprouter as a router for Go ... now with Angular, I should be able to copy & paste a URL into the browser and Angular should handle the according routes, right?
I have a "/login" route. Which works if the route gets accessed via the front-end .... but doesn't if I type in "mypage.com/login" into the browser, getting a 404.
Go routing basically is only doing
router.NotFound = http.FileServer(http.Dir("./public"))
Which works for the "/" route, but not for anything else. Which seems to be correct. But how do I setup the routing correctly, so Angular handles all the routing?
答案1
得分: 13
这是我使用标准Go库的代码,路由功能很好。
请查看这里的Adapt函数
// 创建一个新的ServeMux
mux := http.NewServeMux()
// 创建用于静态文件服务的路径
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"))))
// 处理API请求**
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)
// 处理其他请求,只渲染SPA的HTML文件,
// 允许Angular在除API和自身所需文件之外的任何路径上进行路由
// 这里的顺序很关键。HTML文件应包含类似于<base href="/">的base标签,
// href应与HandleFunc下面的路径匹配
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "html/index.html")
})
英文:
This is what I am using with the standard Go library, and routing works great.
Check out the Adapt function here
// 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")
})
答案2
得分: 2
你可以直接使用http
包。
首页
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./public/index.html")
})
这将在所有不匹配路由的请求上提供index.html文件。
文件服务器
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("./public"))))
这将从public目录中提供所有文件。
不要忘记启动你的服务器
http.ListenAndServe(":8000", nil)
英文:
You can use the http
package directly.
Index page
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./public/index.html")
})
This will serve the index.html file on all requests that don't match a route.
File server
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("./public"))))
This will serve all files from the public directory.
Don't forget to start your server
http.ListenAndServe(":8000", nil)
答案3
得分: 1
使用goji微框架
https://github.com/zenazn/goji
它很容易使用
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()
}
这段代码可以工作,你只需要导入相应的包。
英文:
use goji micro framwork
https://github.com/zenazn/goji
It's easy to use
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()
}
this code works, you need just make imports
答案4
得分: 0
我遇到了完全相同的404问题。这个路由是html5mode。你需要在你的app.yaml文件中告诉处理程序。在这里检查我对Tour of Heroes项目的分支https://github.com/nurp/angular2-tour-of-heroes
将以下内容添加到你的app.yaml文件可能会解决这个问题。
- url: /.*
static_files: index.html
upload: index.html
英文:
I had the exact 404 problem. This routing is html5mode. You need to tell handlers in your app.yaml. Check my fork of Tour of Heroes project here https://github.com/nurp/angular2-tour-of-heroes
adding this to your app.yaml might solve the problem.
- url: /.*
static_files: index.html
upload: index.html
答案5
得分: 0
请定义一个路由器的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()
// 处理Angular
router.NotFound = http.HandlerFunc(angularHandler)
// 提供静态文件
router.ServeFiles("/*filepath", http.Dir("./public"))
log.Fatal(http.ListenAndServe(":3000", router))
}
以上代码定义了一个路由器,当找不到匹配的路由时,将使用angularHandler
函数来提供Angular的index.html文件。同时,还提供了静态文件的服务,将./public
目录下的文件提供给客户端。最后,使用http.ListenAndServe
函数在端口3000上启动服务器。
英文:
Please define router.Notfound handler to serve angular index.html file.
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))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论