第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

我可以在一臺(tái)服務(wù)器上托管 Angular2 前端和 Golang 后端嗎

我可以在一臺(tái)服務(wù)器上托管 Angular2 前端和 Golang 后端嗎

Go
守候你守候我 2021-12-20 15:56:30
我想用 Golang 創(chuàng)建 RESTful API,用 Angular2 創(chuàng)建前端。將通過(guò) http 請(qǐng)求進(jìn)行通信。Angular2 將向 Golang API 發(fā)送請(qǐng)求。我知道對(duì)于 Angular2,我應(yīng)該運(yùn)行自己的 http 服務(wù)器來(lái)進(jìn)行路由和服務(wù)。我可以在一臺(tái)主機(jī)上運(yùn)行 Golang 服務(wù)器,在另一臺(tái)主機(jī)上運(yùn)行 Angular2 服務(wù)器并將它們連接在一起嗎?
查看完整描述

2 回答

?
侃侃爾雅

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超16個(gè)贊

Angular2 應(yīng)用程序?qū)?yīng)一組靜態(tài)文件(依賴項(xiàng)和應(yīng)用程序代碼)。要讓 Go 為您的應(yīng)用程序提供服務(wù),您需要添加一些代碼來(lái)提供這些文件。

似乎有可能。請(qǐng)參閱此鏈接:

編輯

關(guān)注您的評(píng)論:

如果您想在一臺(tái)服務(wù)器上托管 Angular2 和 golang。例如,我將可以通過(guò)鏈接 mywebsite.com 訪問(wèn)網(wǎng)站并訪問(wèn) golang api api.mywebsite.com

我看不出有什么理由不這樣做。請(qǐng)注意在您的 API 中支持 CORS(在響應(yīng)中發(fā)送 CORS 標(biāo)頭并支持預(yù)檢請(qǐng)求)。請(qǐng)參閱這些鏈接:


查看完整回答
反對(duì) 回復(fù) 2021-12-20
?
幕布斯7119047

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個(gè)贊

使用標(biāo)準(zhǔn)庫(kù)實(shí)際實(shí)現(xiàn)


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

})


查看完整回答
反對(duì) 回復(fù) 2021-12-20
  • 2 回答
  • 0 關(guān)注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)