Serving a single page app

polaris · · 551 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m trying to serve a single page react app from a basic net/http server (I&#39;m using mux router) but all I&#39;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&#39;m trying to serve an html file as the root of my react project, and http.FileServer doesn&#39;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&#39;t think using a full fledged framework is at all necessary)</p> <hr/>**评论:**<br/><br/>hopsnob: <pre><p>So I&#39;ve gotten the index and other static files to render/load (as seen in the SO answer) however I&#39;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 ( &#34;log&#34; &#34;net/http&#34; &#34;os&#34; ) func main() { dir, _ := os.Getwd() fs := http.FileServer(http.Dir(dir)) http.Handle(&#34;/&#34;, fs) http.HandleFunc(&#34;/filter/&#34;, func(w http.ResponseWriter, r *http.Request){ http.ServeFile(w, r, &#34;index.html&#34;); }); log.Println(&#34;Serving &#34; + dir) http.ListenAndServe(&#34;:8080&#34;, 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 &#34;/filter/&#34;?</p></pre>caseynashvegas: <pre><p>In this case they did. </p></pre>caseynashvegas: <pre><p>And on other apps I&#39;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 &#34;/&#34;. </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&#39;t being served once I got my index showing up (I&#39;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&#39;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 &#34;/&#34; to any path if that is the only route configured. </p> <p>I believe the gorilla router will 404 if you haven&#39;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&#39;t see that and ended up using strip prefix and a regex to match any resource files</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

551 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传