Trying to host a Single Page App

xuanbao · · 712 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;ve got my SPA (React running with multiple routes. eg, &#34;localhost:3000/&#34;, and you can click on a link to route to &#34;localhost:3000/about&#34; but if you go directly to &#34;localhost:3000/about&#34; in the browser, you get 404&#39;d. How can I get Go to accept JUST &#34;/&#34;, routing to Index, and everything after that is still part of &#34;/&#34;, routing the SPA instead of trying to load a page hosted at &#34;/about&#34;? Tried multiple ways, I can&#39;t seem to get anything to work.</p> <hr/>**评论:**<br/><br/>HarveyKandola: <pre><p>This is how you would do it with Gorilla Mux (we do the same thing with EmberJS):</p> <pre><code>router := mux.NewRouter() router.HandleFunc(&#34;/{rest:.*}&#34;, emberHandler) func emberHandler(w http.ResponseWriter, r *http.Request) { emberView := template.Must(template.ParseFiles(&#34;index.html&#34;)) if err := emberView.Execute(w, nil); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } </code></pre> <p>Does this help?</p></pre>natdm: <pre><p>Any idea on how to do it with the default libs, or <a href="https://github.com/julienschmidt/httprouter" rel="nofollow">https://github.com/julienschmidt/httprouter</a> ? </p></pre>HarveyKandola: <pre><p>See this: <a href="https://golang.org/pkg/net/http/#example_ServeMux_Handle" rel="nofollow">https://golang.org/pkg/net/http/#example_ServeMux_Handle</a></p></pre>elithrar_: <pre><p>Render the template outside the handler; re-parsing on every request is pretty expensive. </p></pre>stroborobo: <pre><p>Ok, let&#39;s make this simple :) I guess you&#39;re using an http.FileSystem to serve all your files.</p> <pre><code>package static import ( &#34;net/http&#34; &#34;os&#34; &#34;path&#34; ) func Handler() http.Handler { return http.FileServer(&amp;indexWrapper{assets}) } type indexWrapper struct { assets http.FileSystem } func (i *indexWrapper) Open(name string) (http.File, error) { ret, err := i.assets.Open(name) if !os.IsNotExist(err) || path.Ext(name) != &#34;&#34; { return ret, err } return i.assets.Open(&#34;index.html&#34;) } </code></pre></pre>natdm: <pre><p>Better idea of what I&#39;m trying to accomplish.</p> <pre><code> package main import ( &#34;log&#34; &#34;net/http&#34; &#34;github.com/julienschmidt/httprouter&#34; &#34;html/template&#34; ) func main() { router := httprouter.New() router.GET(&#34;/public/bundle.js&#34;, func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { http.ServeFile(w, r, &#34;client/public/bundle.js&#34;) }) router.GET(&#34;/about&#34;, func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { tmpl := template.Must(template.ParseFiles(&#34;client/index.html&#34;)) tmpl.Execute(w, nil) }) router.GET(&#34;/&#34;, func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { tmpl := template.Must(template.ParseFiles(&#34;client/index.html&#34;)) tmpl.Execute(w, nil) }) log.Fatal(http.ListenAndServe(&#34;:3000&#34;, router)) } </code></pre> <p>/ and /about are pages on the SPA, where Bundle is my main package. I&#39;d rather serve anything under /public and anything with an actual route go to the SPA. </p></pre>thepciet: <pre><p>Do you need the third party router? With the default by handling &#39;/&#39; you get all requests to &#39;/&#39; and anything in that subtree (like &#39;/about&#39;).</p> <pre><code>http.HandleFunc(&#34;/&#34;, func(w http.ResponseWriter, r *http.Request) { if r.Method != &#34;GET&#34; {...} if r.URL.Path == &#34;/&#34; {...} else if r.URL.Path == &#34;/about&#34; {...} ... tmpl.Execute(w, nil) }) </code></pre></pre>denmaradi: <pre><p>Why do have same handler for both &#34;/&#34; and &#34;/about&#34;? Both needs to have their own separate handlers. </p> <p>When someone goes to localhost/about, &#34;about&#34; handler needs to respond not &#34;/&#34;.</p> <p>Additionally your js app can send special headers that you can parse on your middleware or something to distinguish between SPA call and normal call.</p></pre>stroborobo: <pre><p>Just curious, why using templates if you&#39;ve got everything on frontend side with React?</p></pre>natdm: <pre><p>I don&#39;t have to. I could just serve the index.html. Was just cycling through multiple ideas.</p></pre>

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

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