2016-03-09 57 views

Odpowiedz

6

Aplikacje Angular2 odpowiadają zestawowi plików statycznych (zależności i kod aplikacji). Aby aplikacja była obsługiwana przez Go, musisz dodać kod służący do wyświetlania tych plików.

Wydaje się możliwe. Zobacz ten link:

Edit

Po swoim komentarzu:

Jeśli chcesz zorganizować Angular2 i golang w jednym serwerze. Na przykład będę miał dostęp do strony internetowej z linkiem mywebsite.com i dostępem do golang api api.mywebsite.com

Nie widzę powodu, aby tego nie robić. Po prostu zachowaj ostrożność, aby wspierać CORS w swoim interfejsie API (wysyłaj nagłówki CORS w odpowiedziach i wspieranych wcześniej zapytaniach). Zobacz te linki:

+0

One mogą obejmować nawet te aktywa do binarnego, wykorzystując takie rzeczy [bindata-assetfs ] (https://github.com/elazarl/go-bindata-assetfs) lub [go.rice] (https://github.com/GeertJohan/go.rice). –

+0

Chcę używać golang i angle2 oddzielnie. Serwer Golang wyśle ​​tylko dane JSON. Angular zażąda danych z apletów serwera golang i renderowania stron internetowych. Czy mogę obsługiwać kątowe i golang na jednym serwerze? Na przykład będę miał dostęp do strony internetowej z linkiem http://mywebsite.com i dostępem do golang api http://api.mywebsite.com – EgorkZe

+0

Nie widzę powodu, aby tego nie robić. Po prostu zachowaj ostrożność, aby wspierać CORS w swoim API. Zobacz http://restlet.com/blog/2015/12/15/understanding-and-using-cors/ –

5

rzeczywista implementacja z biblioteką stadand

type Adapter func(http.Handler) http.Handler 

// Adapt function to enable middlewares on the standard library 
func Adapt(h http.Handler, adapters ...Adapter) http.Handler { 
    for _, adapter := range adapters { 
     h = adapter(h) 
    } 
    return h 
} 

// 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", 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") 
}) 
Powiązane problemy