Go web server + angularJS, how to handle html files?

xuanbao · · 905 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi, I&#39;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&#39;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&#39;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(&#34;/&#34;, http.FileServer(http.Dir(&#34;/app&#34;))) 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(&#34;/events&#34;, es) router.PathPrefix(&#34;/&#34;).Handler(http.StripPrefix(&#34;/&#34;, http.FileServer(http.Dir(&#34;public/&#34;))))</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(&#34;/&#34;).Handler(http.FileServer(http.Dir(&#34;./public&#34;)))</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 &#34;/&#34;, and all API endpoints are behind the &#34;/api&#34; 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

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