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

huangapple go评论90阅读模式
英文:

Can I host Angular2 frontend and Golang backend in one server

问题

我想使用Golang创建一个RESTful API,并使用Angular2创建前端。通信将通过HTTP请求进行。Angular2将向Golang API发送请求。我知道对于Angular2,我应该运行自己的HTTP服务器进行路由和服务。

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

英文:

I want to create RESTful API with Golang and frontend with Angular2.
Communication will be made with http requests. Angular2 will send requests to Golang API's. I know for Angular2 I should run own http server for routing and services.

Can I run Golang server on one host and Angular2 server on another one and connect them together?

答案1

得分: 7

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

这似乎是可能的。请参考以下链接:

编辑

根据您的评论:

> 如果您想在一个服务器上托管Angular2和golang。例如,我将通过链接mywebsite.com访问网站,并通过链接api.mywebsite.com访问golang API。

我看不出任何不这样做的理由。只需小心在API中支持CORS(在响应中发送CORS头并支持预检请求)。请参考以下链接:

英文:

Angular2 applications correspond to a set of static files (dependencies and application code). To have your application served by Go, you need to add some code to serve these files.

It seems possible. See this link:

Edit

Following your comment:

> 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

I can't see any reason not to do that. Just be careful to support CORS in your API (send the CORS headers in the response and support prefligthed requests). See these links:

答案2

得分: 6

实际实现与标准库

type Adapter func(http.Handler) http.Handler

// 适配函数以在标准库上启用中间件
func Adapt(h http.Handler, adapters ...Adapter) http.Handler {
    for _, adapter := range adapters {
        h = adapter(h)
    }
    return h
}

// 创建新的服务 mux
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", 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="/" /> 的基本标签
// 此处的 href 应与下面的 HandleFunc 路径匹配
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, "html/index.html")
})

以上是使用标准库的实际实现代码。

英文:

Actual implementation with the stadand library

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(&quot;/node_modules/&quot;, http.StripPrefix(&quot;/node_modules&quot;, http.FileServer(http.Dir(&quot;./node_modules&quot;))))
mux.Handle(&quot;/html/&quot;, http.StripPrefix(&quot;/html&quot;, http.FileServer(http.Dir(&quot;./html&quot;))))
mux.Handle(&quot;/js/&quot;, http.StripPrefix(&quot;/js&quot;, http.FileServer(http.Dir(&quot;./js&quot;))))
mux.Handle(&quot;/ts/&quot;, http.StripPrefix(&quot;/ts&quot;, http.FileServer(http.Dir(&quot;./ts&quot;))))
mux.Handle(&quot;/css/&quot;, http.StripPrefix(&quot;/css&quot;, http.FileServer(http.Dir(&quot;./css&quot;))))
// Do your api stuff**
mux.Handle(&quot;/api/register&quot;, Adapt(api.RegisterHandler(mux),
api.GetMongoConnection(),
api.CheckEmptyUserForm(),
api.EncodeUserJson(),
api.ExpectBody(),
api.ExpectPOST(),
))
mux.HandleFunc(&quot;/api/login&quot;, api.Login)
mux.HandleFunc(&quot;/api/authenticate&quot;, api.Authenticate)
// Any other request, we should render our SPA&#39;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
// &lt;base href=&quot;/&quot;&gt; *href here should match the HandleFunc path below 
mux.HandleFunc(&quot;/&quot;, func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, &quot;html/index.html&quot;)
})

huangapple
  • 本文由 发表于 2016年3月9日 16:01:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/35886032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定