我制作了一个应用程序,需要为多个路由提供相同的文件,因为前端是React应用程序。我一直在使用Gorilla Mux作为路由器。
文件结构如下:
main.go
build/
| index.html
| service-worker.js
static/
| js/
| main.js
| css/
| main.css
假设文件位于文件目录的根目录下,因此在html文件中请求它们时,它们被称为'/static/js/main.js'。
在我的主要路由中,它们被定义如下:
r.PathPrefix("/student").Handler(http.StripPrefix("/student",http.FileServer(http.Dir("build/")))).Methods("GET")
r.PathPrefix("/").Handler(http.FileServer(http.Dir("build/"))).Methods("GET")
这样做可以使index.html文件在'/'和'/student'路由中都能被访问到。如果我反过来设置,'/student'路径就会出现404错误。所以我的问题是是否有另一种方法可以为这两个路由提供相同的内容,从而避免为我的web应用程序中的每个视图定义一个路由。