Serve static content on root and rest on /api

≡放荡痞女 提交于 2019-12-24 08:01:49

问题


I'm using httprouter for parsing some parameters from the path in api calls:

router := httprouter.New()
router.GET("/api/:param1/:param2", apiHandler)

And wanted to add some files to the root (/) to serve. It's just index.html, script.js and style.css. All in a local directory called static

router.ServeFiles("/*filepath", http.Dir("static"))

So that I can go with the browser to localhost:8080/ and it will serve index.html and the js from the browser will call the /api/:param1/:param2

But this path conflicts with the /api path.

panic: wildcard route '*filepath' conflicts with existing children in path '/*filepath'


回答1:


As others pointed out, this is not possible using only github.com/julienschmidt/httprouter.

Note that this is possible using the multiplexer of the standard library, as detailed in this answer: How do I serve both web pages and API Routes by using same port address and different Handle pattern

If you must serve all your web content at the root, another viable solution could be to mix the standard router and julienschmidt/httprouter. Use the standard router to register and serve your files at the root, and use julienschmidt/httprouter to serve your API requests.

This is how it could look like:

router := httprouter.New()
router.GET("/api/:param1/:param2", apiHandler)

mux := http.NewServeMux()
mux.Handle("/", http.FileServer(http.Dir("static")))
mux.Handle("/api/", router)

log.Fatal(http.ListenAndServe(":8080", mux))

In the example above, all requests that start with /api/ will be forwarded to the router handler, and the rest will be attempted to handle by the file server.



来源:https://stackoverflow.com/questions/48259630/serve-static-content-on-root-and-rest-on-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!