英文:
React 404 on refresh or direct url path in golang server
问题
我有这段代码用于提供React构建的应用程序:
package main
import (
"fmt"
"log"
"net/http"
"time"
)
func main() {
r := http.NewServeMux()
r.HandleFunc(`^/`, index)
buildHandler := http.FileServer(http.Dir("build"))
r.Handle("/", buildHandler)
srv := &http.Server{
Handler: r,
Addr: ":5000",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
fmt.Println("Server started on PORT 5000")
log.Fatal(srv.ListenAndServe())
}
func index(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "build/index.html")
}
文件夹结构如下:
但是当我刷新页面或直接访问路径时,出现404未找到错误。如何使其跳转到所需路径而不是出现404错误?
英文:
I have this code to serve react build app:
package main
import (
"fmt"
"log"
"net/http"
"time"
)
func main() {
r := http.NewServeMux()
r.HandleFunc(`^/`, index)
buildHandler := http.FileServer(http.Dir("build"))
r.Handle("/", buildHandler)
srv := &http.Server{
Handler: r,
Addr: ":5000",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
fmt.Println("Server started on PORT 5000")
log.Fatal(srv.ListenAndServe())
}
func index(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "build/index.html")
}
Folder structure looks like this:
But I'm getting 404 not found on refresh or when I try to directly go to path. How can I make it go to desired path instead of getting 404?
答案1
得分: 3
你遇到的问题与React(或其他前端框架)使用的客户端路由有关。当你刷新页面或直接访问特定路径时,服务器无法识别该路由,因为它主要提供静态的index.html文件,而不处理客户端路由。
要解决这个问题,你需要配置Go服务器以处理客户端路由,通过将所有请求重定向到index.html文件。这样,React应用程序中的客户端路由可以接管并处理正确的页面显示。
以下是处理客户端路由的修改后的代码:
package main
import (
"fmt"
"log"
"net/http"
"time"
)
func main() {
r := http.NewServeMux()
// 将所有请求重定向到index.html文件
r.HandleFunc("/", index)
// 从“build”目录中提供静态文件
buildHandler := http.FileServer(http.Dir("build"))
r.Handle("/static/", buildHandler)
r.Handle("/js/", buildHandler)
r.Handle("/css/", buildHandler)
srv := &http.Server{
Handler: r,
Addr: ":5000",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
fmt.Println("服务器已启动,端口号为5000")
log.Fatal(srv.ListenAndServe())
}
func index(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "build/index.html")
}
在修改后的代码中,我们通过将根路径("/")的请求重定向到index.html文件来处理它们。此外,我们使用http.FileServer从“build”目录中提供静态文件(JS、CSS等)。通过这样做,你的服务器将正确地为所有路由提供index.html文件,而React应用程序中的客户端路由将负责显示适当的页面。
英文:
The issue you are encountering is related to the client-side routing used by React (or other frontend frameworks). When you refresh the page or directly visit a specific path, the server doesn't recognize the route since it's primarily serving a static index.html file and not handling client-side routing.
To fix this issue, you need to configure your Go server to handle client-side routing by redirecting all requests to the index.html file. This way, the client-side routing in your React app can take over and handle the correct page display.
Here's the modified code to handle client-side routing:
package main
import (
"fmt"
"log"
"net/http"
"time"
)
func main() {
r := http.NewServeMux()
// Redirect all requests to the index.html file
r.HandleFunc("/", index)
// Serve the static files from the "build" directory
buildHandler := http.FileServer(http.Dir("build"))
r.Handle("/static/", buildHandler)
r.Handle("/js/", buildHandler)
r.Handle("/css/", buildHandler)
srv := &http.Server{
Handler: r,
Addr: ":5000",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
fmt.Println("Server started on PORT 5000")
log.Fatal(srv.ListenAndServe())
}
func index(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "build/index.html")
}
In the modified code, we're handling requests to the root path ("/") by redirecting them to the index.html file. Additionally, we're serving the static files (JS, CSS, etc.) from the "build" directory using the http.FileServer. By doing this, your server will correctly serve the index.html file for all routes, and the client-side routing in your React app will take care of displaying the appropriate pages.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论