如何服务React

如何服务React,第1张

如何服务React

在您的情况下,仅需要构建处理程序。它必须指向目录而不是文件。除路由外,其他处理程序已过时。

package mainimport (    "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 mainimport (    "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")}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5586168.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-14
下一篇2022-12-14

发表评论

登录后才能评论

评论列表(0条)

    保存