<p>In the angular2 start guide it's needed to use "npm start" command for running typescript compiler and lite-server. But i don't want to use lite-server for routing web pages, i want to use golang, for example, mux by gorilla. But i don't know how to realize it. I set path to web site folder: r.PathPrefix("/").Handler(http.FileServer(http.Dir("../sitelocation/")))
http.Handle("/", 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: '/auth', name: 'Auth', 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 ("/{whatevs}"). 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'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("/js", http.FileServer(http.Dir("../sitelocation/js")))
r.Handle("/css", http.FileServer(http.Dir("../sitelocation/css")))
// 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("/", r)
}
func notFound(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "../sitelocation/index.html")
}
</code></pre>
<p>Declaimer: I don't use MUX, so I don't know if this is 100% correct.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传