本文翻译自:Separate REST JSON API server and client? [closed]

I'm about to create a bunch of web apps from scratch. 我即将从头开始创建一堆Web应用程序。 (See http://50pop.com/code for overview.) I'd like for them to be able to be accessed from many different clients: front-end websites, smartphone apps, backend webservices, etc. So I really want a JSON REST API for each one. (请参阅http://50pop.com/code获取概述。)我希望能够从许多不同的客户端访问它们:前端网站,智能手机应用程序,后端网络服务等。所以我真的想要一个每个JSON REST API。

Also, I prefer working on the back-end, so I daydream of me keeping my focus purely on the API, and hiring someone else to make the front-end UI, whether a website, iPhone, Android, or other app. 此外,我更喜欢在后端工作,所以我梦想着我完全专注于API,并雇用其他人来制作前端用户界面,无论是网站,iPhone,Android还是其他应用。

Please help me decide which approach I should take: 请帮我决定采取哪种方法:

TOGETHER IN RAILS 一起在铁路上

Make a very standard Rails web-app. 制作一个非常标准的Rails网络应用程序。 In the controller, do the respond_with switch, to serve either JSON or HTML. 在控制器中,执行respond_with开关,以提供JSON或HTML。 The JSON response is then my API. 然后JSON响应是我的API。

Pro: Lots of precedent. 亲:很多先例。 Great standards & many examples of doing things this way. 伟大的标准和许多以这种方式做事的例子。

Con: Don't necessarily want API to be same as web app. Con:不一定要API与Web应用程序相同。 Don't like if/then respond_with switch approach. 不喜欢if / then respond_with切换方法。 Mixing two very different things (UI + API). 混合两个非常不同的东西(UI + API)。

REST SERVER + JAVASCRIPT-HEAVY CLIENT REST SERVER + JAVASCRIPT-HEAVY CLIENT

Make a JSON-only REST API server. 制作仅限JSON的REST API服务器。 Use Backbone or Ember.js for client-side JavaScript to access API directly, displaying templates in browser. 使用Backbone或Ember.js直接访问客户端JavaScript,在浏览器中显示模板。

Pro: I love the separation of API & client. 亲:我喜欢API和客户端的分离。 Smart people say this is the way to go. 聪明的人说这是要走的路。 Great in theory. 理论上很棒。 Seems cutting-edge and exciting. 似乎前沿和令人兴奋。

Con: Not much precedent. 骗局:没有多少先例。 Not many examples of this done well. 这方面的例子并不多。 Public examples (twitter.com) feel sluggish & are even switching away from this approach. 公共示例(twitter.com)感觉迟钝,甚至转向这种方法。

REST SERVER + SERVER-SIDE HTML CLIENT REST服务器+服务器端HTML客户端

Make a JSON-only REST API server. 制作仅限JSON的REST API服务器。 Make a basic HTML website client, that accesses the REST API only. 创建一个基本的HTML网站客户端,仅访问REST API。 Less client-side JavaScript. 减少客户端JavaScript。

Pro: I love the separation of API & client. 亲:我喜欢API和客户端的分离。 But serving plain HTML5 is quite foolproof & not client-intensive. 但是提供纯HTML5非常简单,而且不是客户密集型的。

Con: Not much precedent. 骗局:没有多少先例。 Not many examples of this done well. 这方面的例子并不多。 Frameworks don't support this as well. 框架也不支持这一点。 Not sure how to approach it. 不知道如何处理它。

Especially looking for advice from experience, not just in-theory. 特别是从经验中寻求建议,而不仅仅是在理论上。


#1楼

参考:https://stackoom.com/question/juJl/单独的REST-JSON-API服务器和客户端-关闭


#2楼

I prefer to go the route of #2 and #3. 我更喜欢走#2和#3的路线。 Mainly because #1 violates separation of concerns and intermingles all kinds of stuff. 主要是因为#1违反了关注点的分离并混合了各种各样的东西。 Eventually you'll find the need to have an API end point that does not have a matching HTML page/etc and you'll be up a creek with intermingled HTML and JSON endpoints in the same code base. 最终你会发现需要一个没有匹配的HTML页面/等的API端点,你将在同一个代码库中使用混合的HTML和JSON端点。 It turns into a freaking mess, even if its MVP, you'll have to re-write it eventually because its soo messy that its not even worth salvaging. 它变成了一个疯狂的混乱,即使它的MVP,你将不得不重新写它最终因为它太混乱,甚至不值得打捞。

Going with #2 or #3 allows you to completely have a API that acts the same (for the most part) regardless. 使用#2或#3可以让您完全拥有一个API,无论如何(大多数情况下)都是相同的。 This provides great flexibility. 这提供了很大的灵活性 I'm not 100% sold on Backbone/ember/whatever/etc.js just yet. 我还没有100%在Backbone / ember / what / etc.js上销售。 I think its great, but as we're seeing with twitter this is not optimal. 我认为它很棒,但正如我们在twitter上看到的那样,这不是最佳选择。 BUT... Twitter is also a huge beast of a company and has hundreds of millions of users. 但是...... Twitter也是一家公司的巨大野兽,拥有数亿用户。 So any improvement can have a huge impact to bottom line on various areas of various business units. 因此,任何改进都会对各个业务部门的各个领域的底线产生巨大影响。 I think there is more to the decision than speed alone and they're not letting us in on that. 我认为除了速度之外,还有更多的决定,而且他们不会让我们参与其中。 But thats just my opinion. 但那只是我的个人意见。 However, I do not discount backbone and its competitors. 但是,我不打折骨干及其竞争对手。 These apps are great to use and are very clean and are very responsive (for the most part). 这些应用程序很好用,非常干净,响应速度很快(大部分)。

The third option has some valid allure as well. 第三种选择也有一些有效的诱惑力。 This is where I'd follow the Pareto principle (80/20 rule) and have 20% of your main markup (or vice versa) rendered on the server and then have a nice JS client (backbone/etc) run the rest of it. 这是我遵循帕累托原则(80/20规则)并在服务器上呈现20%的主标记(反之亦然),然后有一个漂亮的JS客户端(骨干/等)运行其余部分。 You may not be communicating 100% with the REST api via the JS client, but you will be doing some work if necessary to make the suer experience better. 您可能无法通过JS客户端与REST API进行100%通信,但如果有必要,您将做一些工作以使更好的体验。

I think this is one of those "it depends" kinds of problems and the answer is "it depends" on what you're doing, whom you're serving and what kind of experience you want them to receive. 我认为这是“依赖于”那些问题之一,答案是“它取决于”你正在做什么,你所服务的人以及你希望他们接受什么样的经历。 Given that I think you can decide between 2 or 3 or a hybrid of them. 鉴于我认为你可以决定2或3或他们的混合。


#3楼

At Boundless , we've gone deep with option #2 and rolled it out to thousands of students. 在Boundless ,我们已经深入了解选项#2并将其推广到成千上万的学生。 Our server is a JSON REST API (Scala + MongoDB), and all of our client code is served straight out of CloudFront (ie: www.boundless.com is just an alias for CloudFront). 我们的服务器是JSON REST API(Scala + MongoDB),我们所有的客户端代码都是直接从CloudFront提供的(即:www.boundless.com只是CloudFront的别名)。

Pros: 优点:

  • Cutting-edge/exciting 新锐/激动人心
  • A lot of bang for your buck: API gives you basis for your own web client, mobile clients, 3rd party access, etc. 为您带来很多好处:API为您自己的Web客户端,移动客户端,第三方访问等提供了基础。
  • exceedingly fast site loading / page transitions 非常快速的网站加载/网页过渡

Cons: 缺点:

  • Not SEO friendly/ready without a lot more work. 没有SEO友好/准备没有更多的工作。
  • Requires top-notch web front-end folk who are ready to cope w/ the reality of a site experience that is 70% javascript and what that means. 需要一流的网络前端用户,他们随时准备应对70%javascript网站体验的现实以及这意味着什么。

I do think this is the future of all web-apps. 我认为这是所有网络应用程序的未来。

Some thoughts for the web front end folks (which is where all the new-ness/challenge is given this architecture): 对Web前端人员的一些想法(这是所有新的/挑战给予这种架构的地方):

  • CoffeeScript. CoffeeScript的。 Much easier to produce high-quality code. 更容易生成高质量的代码。
  • Backbone. 骨干。 Great way to organize your logic, and active community. 组织逻辑和活跃社区的好方法。
  • HAMLC. HAMLC。 Haml + CoffeeScript templates => JS. Haml + CoffeeScript模板=> JS。
  • SASS 上海社会科学院

We've built a harness for our front-end development called 'Spar' (Single Page App Rocketship) which is effectively the asset pipeline from Rails tuned for single page app development. 我们已经为我们的前端开发构建了一个称为“Spar”(单页应用程序Rocketship)的工具,它实际上是Rails为单页面应用程序开发而调整的资产管道。 We'll be open-sourcing within the next couple of weeks on our github page, along with a blog post explaining how to use it and overall architecture in greater detail. 我们将在接下来的几周内在我们的github页面上开源,以及一篇博客文章,详细解释如何使用它和整体架构。

UPDATE: 更新:

With respect to people's concerns with Backbone, I think they are over-rated. 关于人们对Backbone的关注,我认为它们被高估了。 Backbone is far more an organizational principle than it is a deep framework. Backbone更像是一个组织原则,而不是一个深层框架。 Twitter's site itself is a giant beast of Javascript covering every corner-case across millions of users & legacy browsers, while loading tweets real-time, garbage collect, display lots of multimedia, etc. Of all the 'pure' js sites I've seen, Twitter is the odd one out. Twitter的网站本身就是一个巨大的Javascript野兽,涵盖数百万用户和传统浏览器的每个角落,同时实时加载推文,垃圾收集,显示大量多媒体等。在所有'纯'js网站中我都是看到,Twitter是奇怪的一个。 There have been many impressively complicated apps delivered via JS that fare very well. 通过JS提供的许多令人印象深刻的复杂应用程序非常好。

And your choice of architecture depends entirely on your goals. 您选择的架构完全取决于您的目标。 If you are looking for the fastest way to support multiple clients and have access to good front-end talent, investing in a standalone API is a great way to go. 如果您正在寻找支持多个客户端并获得良好前端人才的最快方式,那么投资独立的API是一个很好的方法。


#4楼

I'm about 2 months into a 3 month project which takes the second approach you've outlined here. 我在为期3个月的项目中大约需要2个月,这是你在这里概述的第二种方法。 We use a RESTful API server side with backbone.js on the front. 我们在前面使用带有backbone.js的RESTful API服务器端。 Handlebars.js manages the templates and jQuery handles the AJAX and DOM manipulation. Handlebars.js管理模板,jQuery处理AJAX和DOM操作。 For older browsers and search spiders we've fallen back onto server side rendering, but we're using the same HTML templates as the Handlebars frontend using Mozilla Rhino. 对于较旧的浏览器和搜索蜘蛛,我们已经回到服务器端渲染,但我们使用与使用Mozilla Rhino的Handlebars前端相同的HTML模板。

We chose this approach for many different reasons but are very aware it's a little risky given it hasn't been proven on a wide scale yet. 我们之所以选择这种方法有很多不同的原因,但我们非常清楚它有一点风险,因为它还没有得到大规模的证明。 All te same, everything's going pretty smoothly so far. 一切都相同,到目前为止一切都很顺利。

So far we've just been working with one API, but in the next phase of the project we'll be working with a second API. 到目前为止,我们刚刚使用了一个API,但在项目的下一阶段,我们将使用第二个API。 The first is for large amounts of data, and the second acts more like a CMS via an API. 第一个用于大量数据,第二个用于通过API更像CMS。

Having these two pieces of the project act completely independent of each other was a key consideration in selecting this infrastructure. 让这两个项目完全相互独立是选择这个基础设施的关键考虑因素。 If you're looking for an architecture to mashup different independent resources without any dependencies then this is approach is worth a look. 如果您正在寻找一种架构来混搭不同的独立资源而没有任何依赖性,那么这种方法值得一看。

I'm afraid I'm not a Ruby guy so I can't comment on the other approaches. 我担心我不是Ruby人,所以我不能对其他方法发表评论。 Sometimes it's okay to take a risk. 有时可以承担风险。 Other times it's better to play it safe. 其他时候最好安全地玩。 You'll k ow yourself depending on the type of project. 你会根据项目的类型自己做。

Best of luck with your choice here. 祝你好运。 Keen to see what others share as well. 热衷于了解其他人分享的内容。


#5楼

We use the following variant of #3: Make a JSON-only REST API server. 我们使用#3的以下变体:创建一个仅限JSON的REST API服务器。 Make an HTML website server. 制作HTML网站服务器。 The HTML web server is not, as in your variant, a client to the REST API server. 与您的变体一样,HTML Web服务器不是REST API服务器的客户端。 Instead, the two are peers. 相反,这两个是同行。 Not far below the surface, there is an internal API that provides the functionality that the two servers need. 在表面不远处,有一个内部API,提供两个服务器所需的功能。

We're not aware of any precedent, so it's kind of experimental. 我们不知道任何先例,所以它是一种实验性的。 So far (about to enter beta), it has worked out pretty well. 到目前为止(即将进入测试版)​​,它已经很好地完成了。


#6楼

I like #3 when my website is not going to be a 100% CRUD implementation of my data. 当我的网站不是我的数据的100%CRUD实现时,我喜欢#3。 Which is yet to happen. 这还没有发生。

I prefer sinatra and will just split up the app into a few different rack apps with different purposes. 我更喜欢sinatra,只是将应用程序分成几个不同用途的机架应用程序。 I'll make an API specific rack app that will cover what I need for the API. 我将制作一个API特定的机架应用程序,它将涵盖我对API的需求。 Then perhaps a user rack app that will present my webpage. 然后可能是一个用户机架应用程序,它将显示我的网页。 Sometimes that version will query the API if needed, but usually it just concerns itself with the html site. 有时,如果需要,该版本将查询API,但通常只关注html网站。

I don't worry about it and just do a persistance layer query from the user side if I need it. 我不担心它,如果需要,只需从用户端进行持久层查询。 I'm not overly concerned with creating a complete separation as they usually end up serving different purposes. 我并不过分关注创建完全分离,因为它们通常最终会用于不同的目的。

Here is a very simple example of using multiple rack apps. 这是使用多个机架应用程序的一个非常简单的示例。 I added a quick jquery example in there for you to see it hitting the API app. 我在那里添加了一个快速的jquery示例,以便您看到它在API应用程序中。 You can see how simple it can be with sinatra and mounting multiple rack apps with different purposes. 你可以看到sinatra有多么简单,并且可以安装多个不同用途的机架应用程序。

https://github.com/dusty/multi-rack-app-app https://github.com/dusty/multi-rack-app-app

单独的REST JSON API服务器和客户端? [关闭]相关推荐

  1. kubernetes API服务器的安全防护

    12.1.了解认证机制 启动API服务器时,通过命令行选项可以开启认证插件. 12.1.1.用户和组 了解用户: 分为两种连接到api服务器的客户端: 1.真实的人 2.pod,使用一种称为Servi ...

  2. cgv服务器维护,kubernetes API服务器的安全防护

    12.1.了解认证机制 启动API服务器时,通过命令行选项可以开启认证插件. 12.1.1.用户和组 了解用户: 分为两种连接到api服务器的客户端: 1.真实的人 2.pod,使用一种称为Servi ...

  3. 读取服务器端的一个json文件info.json,显示到客户端,再从客户端输入两个变量contry和city,传到服务器,改变服务器info.json文件的这两个值

    读取服务器端的一个json文件info.json,显示到客户端,再从客户端输入两个变量contry和city,传到服务器,改变服务器info.json文件的这两个值. j son的文件名info.js ...

  4. json 服务器 文件,json属于服务器文件吗

    json属于服务器文件吗 内容精选 换一换 为了防止您的主机被勒索病毒侵害,请创建防护策略,将重点防御的文件添加到防护策略的监控路径中,并启动机器学习.机器学习会自动聚类并收集该策略下的所有服务器的正 ...

  5. ktor框架用到了netty吗_教你如何构建异步服务器和客户端的 Kotlin 框架 Ktor

    Ktor 是一个使用 Kotlin 以最小的成本快速创建 Web 应用程序的框架. Ktor 是一个用于在连接系统(connected systems)中构建异步服务器和客户端的 Kotlin 框架. ...

  6. python构建json_如何使用Python构建JSON API

    python构建json The JSON API specification is a powerful way for enabling communication between client ...

  7. 基于http协议的api接口对于客户端的身份认证方式以及安全措施

    由于http是无状态的,所以正常情况下在浏览器浏览网页,服务器都是通过访问者的cookie(cookie中存储的jsessionid)来辨别客户端的身份的,当客户端进行登录服务器也会将登录信息存放在服 ...

  8. 基于http协议的api接口对于客户端的身份认证方式以及安全措施[转]

    基于http协议的api接口对于客户端的身份认证方式以及安全措施 由于http是无状态的,所以正常情况下在浏览器浏览网页,服务器都是通过访问者的cookie(cookie中存储的jsessionid) ...

  9. .NET Core3发布Json API

    我们给DNC3(.NET Core 3)上了一个新包,叫做System.Text.Json(点我下载),支持读写器,DOM(文档对象模型),和序列化,在这篇博文里,我会告诉大家为什么要做这个,这个包怎 ...

最新文章

  1. 使用Windows命令行启动服务
  2. 硅谷蓝图发布销售加速黑科技组合第一期
  3. Java 8:从PermGen到元空间
  4. oracle 满足条件取第一行,sql – 如何只选择满足条件的第一行?
  5. Flash中与xml交互时不显示中文的解决办法
  6. 使用它tshark分析pcap的例子以及scapy下载地址
  7. Parcel极速零配置Web应用打包工具
  8. 从0到1打造推荐系统-架构篇
  9. python numpy array转置_python-Numpy Performance-向量及其转置的外积
  10. nsurl转nsdata_ios 中NSData 拼接成功,但写入本地种只有前面的data
  11. 关于rnn神经网络的loss函数的一些思考
  12. 判断是否是微信浏览器还是企业微信浏览器
  13. 小米手机解锁、线刷、开启root权限详细教程
  14. 深入浅出 HTTP协议
  15. 英文书籍下载网站推荐
  16. ROBOTSTUDIO学习
  17. java笔试题分类集锦
  18. 固态硬盘(SSD)原理及相关介绍
  19. Java实现三角形打印
  20. 北京小汽车出行比例首次下降

热门文章

  1. JSP内置 对象(下)
  2. 九度 1529:棋盘寻宝(递推DP)
  3. 1.4.2 真正的建造者模式(3.2)
  4. 分布式文件系统MooseFs部署(二)
  5. 一道简单而又容易出错的题目
  6. fhq-treap模板
  7. (转载)C,C++,C#三者区别
  8. Qt中与文件目录相关操作
  9. appium自动化测试(5)-一些pyhon操作
  10. POJ1009 Edge Detection