dqfxao2898 2019-09-24 22:16
浏览 374
已采纳

如何服务React

I have a simple React application I would like to serve from my Go server back end. I hear the process is similar to serving a static html file, but I just can't seem to get it to work.

When I try to view the app on the browser it says "This page isn't working" and that "localhost has redirected too many times"

Here is the code where I am running the server locally as well as trying to handle the react application

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


}

Here is the code for the index route

func index(w http.ResponseWriter, r *http.Request) {
    // not sure if this is necessary
    http.ServeFile(w, r, "index.html")
}

I believe the solution is simple and that I am most likely making a small error somewhere.

  • 写回答

1条回答 默认 最新

  • dtufl26404 2019-09-24 22:34
    关注

    In your case only build handler is needed. It must point to directory not a file. Rest of the handlers are obsolete except the case of routing.

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

    The same can be achieved with the standard library only.

    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")
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题