如何服务 React
How to serve React
我有一个简单的 React 应用程序,我想从我的 Go 服务器后端提供服务。我听说这个过程类似于提供静态 html 文件,但我似乎无法让它工作。
当我尝试在浏览器上查看应用程序时,它显示 "This page isn't working" 和 "localhost has redirected too many times"
这是我在本地 运行 服务器以及尝试处理 React 应用程序的代码
func main() {
r := mux.NewRouter()
// handle app
buildHandler := http.FileServer(http.Dir("./client/build/index.html"))
r.PathPrefix("/").Handler(buildHandler)
staticHandler := http.StripPrefix("/static/", http.FileServer(http.Dir("./client/build/static")))
r.PathPrefix("/static/").Handler(staticHandler)
r.HandleFunc("/", index).Methods("GET")
srv := &http.Server{
Handler: r,
Addr: "127.0.0.1:8080",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
// serve
fmt.Println("Server started on PORT 8080")
log.Fatal(srv.ListenAndServe())
}
这是索引路由的代码
func index(w http.ResponseWriter, r *http.Request) {
// not sure if this is necessary
http.ServeFile(w, r, "index.html")
}
我认为解决方案很简单,我很可能在某处犯了一个小错误。
在您的情况下,只需要构建处理程序。它必须指向目录而不是文件。除路由情况外,其余处理程序已过时。
package main
import (
"fmt"
"github.com/gorilla/mux"
"log"
"net/http"
"time"
)
func main() {
r := mux.NewRouter()
r.HandleFunc("/route1", index).Methods("GET")
r.HandleFunc("/route2", index).Methods("GET")
buildHandler := http.FileServer(http.Dir("client/build"))
r.PathPrefix("/").Handler(buildHandler)
srv := &http.Server{
Handler: r,
Addr: "127.0.0.1:8080",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
fmt.Println("Server started on PORT 8080")
log.Fatal(srv.ListenAndServe())
}
func index(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "client/build/index.html")
}
只有标准库才能实现同样的效果。
package main
import (
"fmt"
"log"
"net/http"
"time"
)
func main() {
r := http.NewServeMux()
r.HandleFunc("/route1", index)
r.HandleFunc("/route2", index)
buildHandler := http.FileServer(http.Dir("client/build"))
r.Handle("/", buildHandler)
srv := &http.Server{
Handler: r,
Addr: "127.0.0.1:8080",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
fmt.Println("Server started on PORT 8080")
log.Fatal(srv.ListenAndServe())
}
func index(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "client/build/index.html")
}
我有一个简单的 React 应用程序,我想从我的 Go 服务器后端提供服务。我听说这个过程类似于提供静态 html 文件,但我似乎无法让它工作。
当我尝试在浏览器上查看应用程序时,它显示 "This page isn't working" 和 "localhost has redirected too many times"
这是我在本地 运行 服务器以及尝试处理 React 应用程序的代码
func main() {
r := mux.NewRouter()
// handle app
buildHandler := http.FileServer(http.Dir("./client/build/index.html"))
r.PathPrefix("/").Handler(buildHandler)
staticHandler := http.StripPrefix("/static/", http.FileServer(http.Dir("./client/build/static")))
r.PathPrefix("/static/").Handler(staticHandler)
r.HandleFunc("/", index).Methods("GET")
srv := &http.Server{
Handler: r,
Addr: "127.0.0.1:8080",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
// serve
fmt.Println("Server started on PORT 8080")
log.Fatal(srv.ListenAndServe())
}
这是索引路由的代码
func index(w http.ResponseWriter, r *http.Request) {
// not sure if this is necessary
http.ServeFile(w, r, "index.html")
}
我认为解决方案很简单,我很可能在某处犯了一个小错误。
在您的情况下,只需要构建处理程序。它必须指向目录而不是文件。除路由情况外,其余处理程序已过时。
package main
import (
"fmt"
"github.com/gorilla/mux"
"log"
"net/http"
"time"
)
func main() {
r := mux.NewRouter()
r.HandleFunc("/route1", index).Methods("GET")
r.HandleFunc("/route2", index).Methods("GET")
buildHandler := http.FileServer(http.Dir("client/build"))
r.PathPrefix("/").Handler(buildHandler)
srv := &http.Server{
Handler: r,
Addr: "127.0.0.1:8080",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
fmt.Println("Server started on PORT 8080")
log.Fatal(srv.ListenAndServe())
}
func index(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "client/build/index.html")
}
只有标准库才能实现同样的效果。
package main
import (
"fmt"
"log"
"net/http"
"time"
)
func main() {
r := http.NewServeMux()
r.HandleFunc("/route1", index)
r.HandleFunc("/route2", index)
buildHandler := http.FileServer(http.Dir("client/build"))
r.Handle("/", buildHandler)
srv := &http.Server{
Handler: r,
Addr: "127.0.0.1:8080",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
fmt.Println("Server started on PORT 8080")
log.Fatal(srv.ListenAndServe())
}
func index(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "client/build/index.html")
}