我可以在一台服务器上托管 Angular2 前端和 Golang 后端吗

Can I host Angular2 frontend and Golang backend in one server

我想用 Golang 创建 RESTful API,用 Angular2 创建前端。 将使用 http 请求进行通信。 Angular2 将向 Golang API 发送请求。我知道对于 Angular2,我应该 运行 拥有用于路由和服务的 http 服务器。

我可以 运行 一台主机上的 Golang 服务器和另一台主机上的 Angular2 服务器并将它们连接在一起吗?

Angular2应用对应一组静态文件(依赖和应用代码)。要让您的应用程序由 Go 提供服务,您需要添加一些代码来提供这些文件。

这似乎是可能的。看到这个 link:

编辑

关注您的评论:

If you want to host Angular2 and golang in one server. For example i will have access to web site with link mywebsite.com and access to golang api api.mywebsite.com

我看不出有什么理由不这样做。请注意在您的 API 中支持 CORS(在响应中发送 CORS headers 并支持预检请求)。请参阅这些 link:

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")
})