我有一个问题,我想在 FileServe 上提供我的主要 AngularJS(Yeoman 部署)应用程序文件夹,/但它会破坏我所有的路由器绑定。有什么办法可以保留它们并保持我的路线完好无损?
在下面的代码中,我仍然需要/app重新绑定其他文件夹,因为我不想过多地使用 Grunt 文件(还),所以为文件夹添加了一些额外的备份路径绑定。
func initializeRoutes() {
// Handle all requests by serving a file of the same name
fileHandler := http.FileServer(http.Dir(*clFlagStaticDirectory))
bowerFileHandler := http.FileServer(http.Dir("../bower_components"))
imagesFileHandler := http.FileServer(http.Dir("../app/images"))
scriptsFileHandler := http.FileServer(http.Dir("../app/scripts"))
stylesFileHandler := http.FileServer(http.Dir("../app/styles"))
viewsFileHandler := http.FileServer(http.Dir("../app/views"))
// Setup routes
mainRoute := mux.NewRouter()
mainRoute.StrictSlash(true)
// mainRoute.Handle("/", http.RedirectHandler("/static/", 302))
mainRoute.PathPrefix("/app").Handler(http.StripPrefix("/app", fileHandler))
mainRoute.PathPrefix("/app/bower_components").Handler(http.StripPrefix("/bower_components", bowerFileHandler))
mainRoute.PathPrefix("/bower_components").Handler(http.StripPrefix("/bower_components", bowerFileHandler))
mainRoute.PathPrefix("/images").Handler(http.StripPrefix("/images", imagesFileHandler))
mainRoute.PathPrefix("/scripts").Handler(http.StripPrefix("/scripts", scriptsFileHandler))
mainRoute.PathPrefix("/styles").Handler(http.StripPrefix("/styles", stylesFileHandler))
mainRoute.PathPrefix("/views").Handler(http.StripPrefix("/views", viewsFileHandler))
// Basic routes
// User routes
userRoute := mainRoute.PathPrefix("/users").Subrouter()
userRoute.Handle("/login", handler(userDoLogin)).Methods("POST")
userRoute.Handle("/logout", handler(userDoLogout)).Methods("GET")
userRoute.Handle("/forgot_password", handler(forgotPassword)).Methods("POST")
// Bind API Routes
apiRoute := mainRoute.PathPrefix("/api").Subrouter()
apiProductModelRoute := apiRoute.PathPrefix("/productmodels").Subrouter()
apiProductModelRoute.Handle("/", handler(listProductModels)).Methods("GET")
apiProductModelRoute.Handle("/{id}", handler(getProductModel)).Methods("GET")
// Bind generic route
http.Handle("/", mainRoute)
}
所以我的目标是将服务/app作为我的/主要路径,但保留我所有的 Mux 路由以赢得 FileServe。因此,如果我有一个/app/users/login文件夹,它不会加载,而是会让路由器获胜。
注意:我的服务纯粹结束了HTTPS,没有结束HTTP。
提前非常感谢!这让我大吃一惊,这是我在完全开始我的前端代码之前需要弄清楚的最后一件事:)。