<p>Hi, I'm new to web programming and got some questions!</p>
<p>My goal is to create a single-page application with angularJS where I can modify the the index.html with additional content. I currently have an emtpy index.html with an ng-view, and I want for starters insert content from login.html into the ng-view. The plan later on is for the client to communicate with the Go server with REST api to fetch resources.</p>
<p>I wonder how the html files should be served to the client from the Go server? I've played around with the http package, where I used Execute/ExecuteTemplate and it works when i dealt with one html file(index.html). I'm lost how to deal with multiple html files where the other html files are content to be added to the index.html whenever something happens on the angularJS side of things. Should I still use Execute/ExecuteTemplate or should I serve all the html files with FileServer?</p>
<hr/>**评论:**<br/><br/>dpfg: <pre><p>You can use FileServer from the http package:
http.Handle("/", http.FileServer(http.Dir("/app")))
This should be enough for simple cases. But for production I would recommend to use separate reverse proxy like nginx. </p></pre>phivuu: <pre><blockquote>
<p>nginx</p>
</blockquote>
<p>EDIT: Looks like i fixed it. The order of the routes are important.
router.Handle("/events", es)
router.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("public/"))))</p>
<p>I use the gorilla mux package to handle routing. I want to serve everything in the public folder and use the following line of code: router.PathPrefix("/").Handler(http.FileServer(http.Dir("./public")))</p>
<p>But my server-sent message(eventsource) seems broken. I was planning to use server-sent messages to notify changes to clients. It works if the pathprefix and a stripprefix is the same as the fileserver path, but I dont want to access the site by localhost:8080/public. I want to type localhost:8080.</p></pre>Injunire: <pre><p>I built a Go application that serves a React Javascript app at "/", and all API endpoints are behind the "/api" route.</p>
<p>Using Gorillia Mux makes it easy, my main route is just a http.FileServer while my API routes are a subrouter.</p>
<p>Check out <a href="https://github.com/MajorMJR/factorio-server-manager/blob/master/routes.go" rel="nofollow">https://github.com/MajorMJR/factorio-server-manager/blob/master/routes.go</a> to see how I structured the mux router.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传