<p>I'm trying to serve a single page react app from a basic net/http server (I'm using mux router) but all I'm seeing is a nice, pretty, clean, white page. I asked a question on SO <a href="http://stackoverflow.com/questions/34866002/serving-a-single-page-application-with-go-net-http-mux">http://stackoverflow.com/questions/34866002/serving-a-single-page-application-with-go-net-http-mux</a>.</p>
<p>The code shown over yonder is quite similar to an api example I found: <a href="http://thenewstack.io/make-a-restful-json-api-go/">http://thenewstack.io/make-a-restful-json-api-go/</a></p>
<p>However I'm trying to serve an html file as the root of my react project, and http.FileServer doesn't seem to be doing the trick.</p>
<p>I know there are a few frameworks that cover this (and even a full fledged react go boilerplate, but in the spirit of learning, I don't think using a full fledged framework is at all necessary)</p>
<hr/>**评论:**<br/><br/>hopsnob: <pre><p>So I've gotten the index and other static files to render/load (as seen in the SO answer) however I'm still struggling getting direct paths to load properly i.e. localhost:8080/whatever 404s even if a react route is properly set up and works when clicking a link takes you there.</p></pre>caseynashvegas: <pre><pre><code>package main
import (
"log"
"net/http"
"os"
)
func main() {
dir, _ := os.Getwd()
fs := http.FileServer(http.Dir(dir))
http.Handle("/", fs)
http.HandleFunc("/filter/", func(w http.ResponseWriter, r *http.Request){
http.ServeFile(w, r, "index.html");
});
log.Println("Serving " + dir)
http.ListenAndServe(":8080", nil)
}
</code></pre>
<p>Here I am serving a SPA using react on the client. You will see here I have a /filter/ route that is client side only so i just serve the index.html.</p></pre>hopsnob: <pre><p>so all of your client side urls begin with "/filter/"?</p></pre>caseynashvegas: <pre><p>In this case they did. </p></pre>caseynashvegas: <pre><p>And on other apps I've used a trick of client side routes all being to a /app/ route and then all REST API calls being under a /api/ route. It really helps me to organize it that way to reduce friction.</p></pre>elingeniero: <pre><p>You use http.FileServer for your js and template files and you just send file index.html at "/". </p>
<p>Make sure your js is referencing the right server path (remember it needs to reference from the FileServer root not your app folder root) and check the browser console for errors...</p></pre>hopsnob: <pre><p>I was using FileServer, however needed to actually handler.ServeHttp(w,r) </p>
<p>Additionally, my other static files weren't being served once I got my index showing up (I've switched the path to /dist at this point). To fix this I added a router.PathPrefix pointing to that same /dist folder</p>
<p>The correct paths were referenced, and the browser console wasn't showing any errors other than 404.</p></pre>elingeniero: <pre><p>Are you using the gorilla router? By default the go ServeMux matches the closest most specific registered route - so will serve "/" to any path if that is the only route configured. </p>
<p>I believe the gorilla router will 404 if you haven't specified a matching route. I think for an SPA you should stick with the default since it has the behaviour you want. </p></pre>hopsnob: <pre><p>Hmm I didn't see that and ended up using strip prefix and a regex to match any resource files</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传