单独的 REST 后端 API 的优势? [英] Advantages of a separate REST backend API?

查看:15
本文介绍了单独的 REST 后端 API 的优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:我是一名初级程序员,自学,希望能制作一个 SPA.我已经开始使用 JavaScript、Jquery、PHP 和 MySQL,现在对所有这些都非常有信心.我从 Ember 开始,现在从 PHP API 转移到 Node.js.这让我更接近 Meteor ......我知道我需要使用 Mongo,但集成正面和背面似乎是明智的,并且有一些优势.

Context: I'm a beginner programmer, self taught in the hope of making a SPA. I've started with JavaScript, Jquery, PHP and MySQL, and now feel pretty confident with all. I've started with Ember, and am now moving away from having a PHP API to Node. Which has then brought me closer to Meteor... I'm aware I'll need to use Mongo instead, but having an integrated front and back seems to be sensible and have some advantages.

所以我的问题是拥有单独的 REST 后端 API(例如 Express)而不是集成的前端/后端(例如 Meteor)有什么优势.

我想到的是,我的应用程序将基于平板电脑/PC,但将来我会想要一个不同的移动版本,这样我就可以使用相同的 API.我意识到上述问题是这个堆栈问题的主要问题,但也许流星开发人员可以澄清这是否确实是一个问题.

One that springs to mind is that my app will be tablet/pc based, but in future I'll want a different mobile version, so I'd be able to use just use the same API. I'm conscious that the above question is the main concern with this stack question, but perhaps if a meteor developer could clarify whether this is indeed a concern.

提前致谢!

推荐答案

Meteor 并没有像您描述的那样真正集成"前端(客户端)和后端(服务器).它仍然将它们保持为两个独立的层.流星的美妙之处(除了非常棒的反应性)在于它在任何地方都使用 Javascript,而不是在客户端使用 JS,在服务器上使用其他一些语言,因此您可以在前端和后端使用相同的 API.虽然 Meteor 做了一些时髦的事情,比如让你在同一个文件中编写客户端和服务器代码,但它仍然需要你区分两者,服务器代码仍然只存储在服务器上,客户端代码仍然向下提供给客户端.

Meteor doesn't really "integrate" the front (client) and backend (server) as you describe. It still maintains them as two separate layers. The beauty of meteor (aside from the insanely awesome reactivity) is that it uses Javascript everywhere, instead of using JS on the client and some other language on the server, so you can use the same APIs on both the front and backend. Although Meteor does snazzy things like let you write client and server code in the same file, it still requires you to distinguish between the two, and server code is still stored only on the server and client-side code is still served down to the client.

Meteor 还很年轻,但是开发者和社区非常活跃,你描述的一切都可以用它来实现.我已经使用 Meteor 大约 6 个月了,它并没有让我失望.我正在开发一个生产级应用程序,该应用程序还需要公开 REST API 以供在移动应用程序中使用,我在 Meteor 上做得非常成功(我刚刚使用 Android 设备上的 REST 端点更新了用户配置文件并观看了它Meteor 应用程序中的实时变化.太酷了!).

Meteor is still young, but the developers and community are very active, and everything you described can be achieved with it at this point. I've been working with Meteor for about 6 months now, and it hasn't let me down yet. I'm working on a production-level application that also requires exposing a REST API for consumption in mobile apps, which I'm doing quite successfully with Meteor (I just updated a user profile using a REST endpoint from an Android device and watched it change in the Meteor app in realtime. So cool!).

我使用了这个很棒的包,RestStop2,用于在meteor 中构建REST API,但不幸的是它被弃用了,所以我发布了一个更新版本.查看在 Meteor 中构建 REST API 的示例.它可以通过 Meteor 包管理器获得:https://atmospherejs.com/nimble/restivus

I was using this great package, RestStop2, for building REST APIs in meteor, but it was unfortunately deprecated, so I released an updated version. Check it out for an example of building REST APIs in Meteor. It's available through the Meteor package manager: https://atmospherejs.com/nimble/restivus

所以要回答您的问题,您总是希望将 REST API 分离到它自己的层中,但这完全可以通过 Meteor 实现.明确地说,你永远不会在你的 Meteor 应用程序中使用这个 REST API.Meteor 使用 DDP(而不是 HTTP),它为您提供了与服务器更直接的连接,因此如果您通过 HTTP 从 Meteor 客户端访问 Meteor 服务器上的数据,那么您就做错了.当然,借助 Meteor,您可以使用来自 REST API 的现有代码.

So to answer your question, you always want to separate the REST API into it's own layer, but that is entirely possible with Meteor. To make it clear, you would never consume this REST API from within your Meteor app. Meteor uses DDP (not HTTP), which gives you a much more direct connection with your server, so you're doing something wrong if you're accessing data on your Meteor server from a Meteor client via HTTP. Of course, with Meteor, you have the advantage of being able to use existing code from your REST API.

有一篇非常好的文章解释了在 Meteor 中编写 REST API 的一些注意事项:http://www.meteorpedia.com/read/REST_API.

There's a really good write-up that explains some of the considerations of writing a REST API in Meteor: http://www.meteorpedia.com/read/REST_API.

这篇关于单独的 REST 后端 API 的优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆