Is it okay to serialize my data, send it to the browser and let javascript handle it?

polaris · · 1338 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>My question isn&#39;t restricted to golang, but golang is the language I use for programming now. I have scratched a bit of django and php in the past. I remember that a dynamic page you embed some sort of framework language specific with html/css/js, and it writes the data it should in the backend on the template file, then send an html file to the user. The browser only does rendering.<br/> What I am trying to do now is serialize the data into json or protobuf. When the browser asks for the data it would get the database query result serialized. Then the javascript code kicks in and puts in a table or whatever I want to do with it. This pattern doesn&#39;t seem stupid for the new backend developper hoobyist I am. And moreover if I want to, I feel like I could write a native desktop app or smartphone app, ask for the same serialized data by http, process it and give it to the user without rewriting my backend. </p> <p>My question is there any reall downside or reason to not do it this way, and use things like templates that are calculated by the backend?</p> <hr/>**评论:**<br/><br/>: <pre><p>[deleted]</p></pre>rat9988: <pre><p>Thank you very much, this is what I wanted.<br/> I guess for whatever I implement it&#39;s up to me to check if the performance are on par with expectations :)</p></pre>darkmagician2: <pre><p>Check this out :D <a href="https://improbable.io/games/blog/grpc-web-moving-past-restjson-towards-type-safe-web-apis" rel="nofollow">https://improbable.io/games/blog/grpc-web-moving-past-restjson-towards-type-safe-web-apis</a></p></pre>rat9988: <pre><p>Thanks it looks useful. I&#39;ll have to reread it later because I didn&#39;t understand everything :)</p></pre>luckyleprechaun98: <pre><p>Tried this out last weekend integrated with Mobx in a React frontend. Works pretty well. I&#39;m not sure if it has tons of advantages for typical crud apps, but if you&#39;re already on the Typescript train, this is a good option for taking advantage of HTTP/2 and strongly typed interfaces to the backend. </p></pre>darkmagician2: <pre><p>That&#39;s awesome you tried it out! I think the strongly typed plus the very efficient transfer of data using protobufs and http/2 would be pretty big advantages, did you see any negatives?</p></pre>luckyleprechaun98: <pre><p>I build the front end with React and the API server in Go. This has some advantages:</p> <ul> <li><p>Can build fairly complicated user interfaces in React</p></li> <li><p>Server-side is simpler and can serve multiple front ends (e.g. mobile apps)</p></li> <li><p>Performance is pretty good. </p></li> </ul> <p>Disadvantages:</p> <ul> <li>The special hell that is JS dependency management and build systems</li> </ul> <p>That&#39;s a huge disadvantage, so unless you need the kind of dynamic UI in which React excels, it&#39;s not worth it. </p></pre>rat9988: <pre><p>My question will seem stupid, but why would I go through js dependency management? until now I just downloaded js files like jquery or bootstrap ones and put them in the same folder.</p> <p>Are things more complicated than I think?</p></pre>Ballresin: <pre><p>When you get into React or other frameworks, you start to get into &#34;JS Build systems&#34; which is just tons of &#34;tooling&#34; that traspiles, shims older browsers, and hooks things together. It is all hideously overcomplicated and generally unnecessary. Sometimes it feels like there are folks that simply enjoy the chain of incantations that make them appear to have achieved greater mastery. Just seems silly to me.</p> <p>I use jQuery for DOM manipulation (which is now generally frowned upon by the cool kids) and either PHP or Go for the backend. There&#39;s nothing wrong with this, and has served me well for years.</p> <p>I&#39;m very productive with the &#34;stack&#34; I choose to use, and it eliminates dependencies apart from jQuery and sometimes Bootstrap. My rule for tools is &#34;if the instructions mention node.js, I&#39;m out&#34;.</p> <p>All that said, I&#39;m happier working on the back-end than front-end. I&#39;m certainly not a designer, so YMMV.</p></pre>rat9988: <pre><p>Thank you for sharing your experience, it really means a lot to me!</p></pre>SeerUD: <pre><p>I can also add on here, modern JS is very complicated, and can also confirm, something like jQuery, or what you already do is fine. That being said, there are also pretty huge advantages to using something like Angular or React over jQuery for and entirely frontend JS client.</p> <p>Things like routing, and handling browser interaction and navigation will be easier, templating too. You may find you have a more organised folder structure, and that there are clearer definitions of the components that comprise your application.</p> <p>All in all, it&#39;s a tradeoff - but it is also a pretty steep learning curve. Use what you feel most comfortable with if you&#39;re pushed for time, if you have tons of time, then maybe just try something like Angular or React out. For Angular, there are things like Angular CLI that will help you start a project very easily.</p> <p>If you&#39;ve not done much NodeJS stuff before (like using Gulp / Grunt, etc.) then I&#39;d definitely recommend that you use <a href="https://github.com/creationix/nvm" rel="nofollow">NVM</a> to manage your NodeJS installation. You can switch versions easily, and handling installing things like LTS releases is easy, e.g. <code>$ nvm install lts/boron</code>. You can also use <code>.nvmrc</code> files in repositories to make it easy to switch versions per-project (just use <code>$ nvm use</code> in a directory where that file exists and it will detect the version and use it, if it&#39;s not already installed, you can just run <code>$ nvm install</code> in that directory to install it, then use it after).</p></pre>luckyleprechaun98: <pre><p>React definitely has advantages when you are building big or complicated apps. The tooling around it is a bit difficult to get started with, but generally works well once you get past the initial hurdle. </p> <p>There&#39;s nothing wrong with going just straight bootstrap and jQuery, but when you have something really complicated or specialized, React can be great for building those kind of apps. </p> <p>It just takes a desire to get over the initial learning curve. Other good options are Ember or Vue.js. </p></pre>gentleman_tech: <pre><p>as others have mentioned, React and other frameworks require tooling and nightmareish support constructs.</p> <p>However, the benefit (once you get them all working) is also minification/uglification. You pass less data in less http round-trips to the client, which means your pages load faster. And your source code is uglified, which helps with script kiddies picking it apart and using bits of it in ways you don&#39;t want. This is a serious benefit.</p></pre>mini_eggs: <pre><p>Do you have experience with create-react-app? It&#39;s goal is to ease this pain point.</p> <p>I&#39;ve used it with Go backends and it&#39;s been great.</p></pre>

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

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