Reputation:
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?
Upvotes: 14
Views: 16091
Reputation: 2096
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))
}
Upvotes: 0
Reputation: 7758
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")
})
Upvotes: 13
Reputation: 1275
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
Upvotes: 0
Reputation: 976
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
Upvotes: 1
Reputation: 12280
You can use the http
package directly.
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.
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)
Upvotes: 2