在不同的路線上運行 SPA
小唯快跑啊
2022-06-27 16:09:28
所以我已經使用 Gorilla/Mux 在 Go 中設置了我的 SPA,但我想在不同的路由上運行它,這樣我就可以將我的兩個 Kubernetes 服務連接在一起(2 個不同的 SPA)。難道我必須將我的 kubernetes 服務更改為 /stock 嗎?我的一個服務在常規路徑“/”上運行,我想添加第二個服務,以便當用戶輸入“https://URL/stock”時,我的第二個 SPA 服務(希望第二個 spa 在 /stock 上運行)我知道一種方法就是將我的代碼和諸如此類的東西合并到我的第一個服務中,但我想知道我是否可以這樣做?main.gotype spaHandler struct { staticPath string indexPath string}func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { path, err := filepath.Abs(r.URL.Path) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } path = filepath.Join(h.staticPath, r.URL.Path) _, err = os.Stat(path) if os.IsNotExist(err) { http.ServeFile(w, r, filepath.Join(h.staticPath, h.indexPath)) return } else if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r)}func main() { router := mux.NewRouter().StrictSlash(true) //Serve Other Routes Above PathPrefix router.HandleFunc("/api/findStock", findStock) spa := spaHandler{staticPath: "./stock/build", indexPath: "index.html"} router.PathPrefix("/").Handler(spa) <------ any way to serve on /stock??? svr := &http.Server{ Handler: router, Addr: ":8080", WriteTimeout: 15 * time.Second, ReadTimeout: 15 * time.Second, } log.Fatal(svr.ListenAndServe())}應用程序.jsclass App extends Component { render(){ return ( <Router> <div> <section> <NavBar/> <Switch> <Route exact path='/' component={Home}/> </Switch> </section> </div> </Router> ); }}export default App;我知道我可以在 App.js 中更改 /stock 的路徑,但是當我到達 localhost:8080/ 時,它仍然顯示我的導航欄所以當我去 /stock 端點時,我得到來自“www.url.com//static/css/2.d9ad5f5c.chunk.css”的資源由于 MIME 類型(“text/plain”)不匹配(X-Content-Type-Options: nosniff)而被阻止我認為這是因為我的 Stock Service 默認路徑在 main.go 中設置為 / 所以它試圖從那里提供文件,但在網絡上我試圖從 /stock 訪問它
查看完整描述