Angular2 with GoLang routing

xuanbao · · 1582 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>In the angular2 start guide it&#39;s needed to use &#34;npm start&#34; command for running typescript compiler and lite-server. But i don&#39;t want to use lite-server for routing web pages, i want to use golang, for example, mux by gorilla. But i don&#39;t know how to realize it. I set path to web site folder: r.PathPrefix(&#34;/&#34;).Handler(http.FileServer(http.Dir(&#34;../sitelocation/&#34;))) http.Handle(&#34;/&#34;, r) and if i go to localhost:3000 i see index.html web page. But in angular main.component.ts i set in RoutConfig redirect to localhost:3000/auth page as default:{ path: &#39;/auth&#39;, name: &#39;Auth&#39;, component: AuthComponent, useAsDefault: true } and if i refresh web page i get 404 error, because /auth path not known by mux gorilla. And finally the question: How should i configure golang mux/gorilla server for correct routing Angular2 pages? Thank you, i hope you can help me</p> <hr/>**评论:**<br/><br/>daveddev: <pre><p>If I understand you correctly, you will need to register a handler with a wildcard route (&#34;/{whatevs}&#34;). This should act as a catch-all handler for the SPA.</p></pre>EgorkZe: <pre><p>Yes! How can i do it? All routes locating in main AppComponent, and should i redirect all requests to index.html wich will rout to needed paths and display needed templates?</p></pre>tex0: <pre><p>What about a custom handler that always serves the index.html if it doesn&#39;t hit a file?</p></pre>daveddev: <pre><p>That can be implemented by setting the NotFoundHandler field of mux.Router.</p> <p><a href="https://godoc.org/github.com/gorilla/mux#Router" rel="nofollow">https://godoc.org/github.com/gorilla/mux#Router</a></p></pre>Arbaal: <pre><pre><code>func main() { r := mux.NewRouter() r.Handle(&#34;/js&#34;, http.FileServer(http.Dir(&#34;../sitelocation/js&#34;))) r.Handle(&#34;/css&#34;, http.FileServer(http.Dir(&#34;../sitelocation/css&#34;))) // Also include every other folder or static file in the root you want to publish r.NotFoundHandler = http.HandlerFunc(notFound) // Fallthrough for HTML5 routing http.Handle(&#34;/&#34;, r) } func notFound(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, &#34;../sitelocation/index.html&#34;) } </code></pre> <p>Declaimer: I don&#39;t use MUX, so I don&#39;t know if this is 100% correct.</p></pre>

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

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