Javascript 如何为 REST API 后端构建 REST 客户端前端?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28653933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to build a REST client frontend for a REST API backend?
提问by heapoverflow
I've built a REST API backend using Django and am now at the stage of designing a client facing frontend. Though, I can't seem to understand how this client frontend should be structured and which languages it should use.
我已经使用 Django 构建了一个 REST API 后端,现在正处于设计面向客户端的前端的阶段。不过,我似乎无法理解这个客户端前端应该如何构建以及它应该使用哪些语言。
PHP is server-side, and is usually used as the language for building backends. When coupled with a framework such as Codeigniter, it can also be used to play around with sessions, route URLs, and decide which templates to use. Though, I don't believe it can be used to call my REST API to fetch resources (might be wrong here, correct me please if I am).
PHP 是服务器端,通常用作构建后端的语言。当与 Codeigniter 等框架结合使用时,它还可以用于处理会话、路由 URL 并决定使用哪些模板。不过,我不相信它可以用来调用我的 REST API 来获取资源(这里可能有误,请纠正我)。
Javascript is client facing but is used only once the webpage has been fetched from the server. AngularJS is great, but from what I've read, it seems it only helps add very dynamic functionality into already rendered static pages.
Javascript 是面向客户端的,但仅在从服务器获取网页后才使用。AngularJS 很棒,但从我读过的内容来看,它似乎只会帮助将非常动态的功能添加到已经呈现的静态页面中。
I am really open to any ideas, suggestions, and advice based on your experiences creating client frontends. So, back to my original question, how does one structure a REST client frontend, which language is best for this goal, and if which frameworks should one consider to use?
根据您创建客户端前端的经验,我非常愿意接受任何想法、建议和建议。那么,回到我最初的问题,如何构建 REST 客户端前端,哪种语言最适合此目标,以及是否应该考虑使用哪些框架?
Update 1
更新 1
Someone asked whether this client frontend will be run in a browser -- the answer is yes, it will. I am trying to build a Twitter-like web client frontend that interacts with a Twitter-like REST API backend. Basically, everything you see there when you go on Twitter's website.
有人问这个客户端前端是否会在浏览器中运行——答案是肯定的,它会。我正在尝试构建一个类似 Twitter 的 Web 客户端前端,该前端与类似 Twitter 的 REST API 后端交互。基本上,当您访问 Twitter 网站时,您在那里看到的所有内容。
回答by Tivie
Since it is a browser frontend I would go with HTML/JavaScript only. No need to use PHP or any server side language IMHO. This has the advantage of being extremely portable.
由于它是一个浏览器前端,我只会使用 HTML/JavaScript。恕我直言,无需使用 PHP 或任何服务器端语言。这具有非常便携的优点。
I would also use a JS framework for that purpose ( the trend nowadays seems to be angular).
我也会为此使用 JS 框架(现在的趋势似乎是有角度的)。
回答by Andy Preston
REST really, really isn't new. It's been a part of HTTP at least as far back as HTTP 1.1
REST 真的,真的不是什么新鲜事。至少早在 HTTP 1.1 时,它就已经是 HTTP 的一部分
Have a look at this question: Backbone.js frontend with RESTful Rails backend?the top answer lists 20 possible frameworks for building a front end.
看看这个问题:Backbone.js frontend with RESTful Rails backend? 最佳答案列出了用于构建前端的 20 个可能的框架。
回答by heapoverflow
Thanks for your help, everyone. Found exactly what I was looking for here: http://docs.python-requests.org/en/latest/
谢谢大家的帮助。在这里找到了我正在寻找的内容:http: //docs.python-requests.org/en/latest/
A nice little library for Python that allows me to basically make calls to a REST backend from within a Django application, which serves as my frontend.
一个不错的 Python 小库,它允许我从作为前端的 Django 应用程序中调用 REST 后端。
AngularJS will also be used for to make the static pages that Django returns more dynamic. Why? Because AngularJS by itself can be the complete solution only ifyour whole site consists of one page. If you have multiple pages where each one has it's own set of dynamic elements, you need a combination of Django and AngularJS.
AngularJS 还将用于使 Django 返回的静态页面更加动态。为什么?因为只有当您的整个站点由一页组成时,AngularJS 本身才能成为完整的解决方案。如果您有多个页面,其中每个页面都有自己的一组动态元素,那么您需要结合使用 Django 和 AngularJS。
Apparently REST is still quite new and it seems there aren't many people that have stumbled upon this very fundamental question like I have.
显然 REST 仍然很新,似乎没有多少人像我一样偶然发现了这个非常基本的问题。
Once again, thanks!
再次感谢!

