Home  >  Article  >  Backend Development  >  javascript - How to design a backend that only provides api?

javascript - How to design a backend that only provides api?

WBOY
WBOYOriginal
2016-07-06 13:53:291026browse

For example, if the front-end uses a framework such as react or vue, I just want to get the corresponding data in the background through ajax. What should I do? Or how to choose the backend? I've written something similar using Django before, but I still feel like I can't break away from the views provided by Django itself.

Reply content:

For example, if the front-end uses a framework such as react or vue, I just want to get the corresponding data in the background through ajax. What should I do? Or how to choose the backend? I've written something similar using Django before, but I still feel like I can't break away from the views provided by Django itself.

http://www.django-rest-framework.org/

1. First of all, the choice of backend language is not important. It can be implemented in any language. The key depends on which language you are familiar with. Which language you are familiar with will get started faster.
2. The effect you want to achieve should be separation of front and back. The backend provides an API, which returns the data format required by the frontend, and the frontend obtains data through the API. The frontend renders the page itself.
3. Because I work in PHP, I recommend using PHP to build the backend. The background is separated by MVC. Since the backend only provides API, V can be omitted. First, it is recommended that you choose a framework that supports configurable routing, then M operates the database and obtains data from the background, and C converts the data into json format and transmits it to the front desk. All you need to do is configure routing according to the rules of the framework, and then write Controller code and Model code. Frameworks include ThinkPHP or Laravel, etc.

Based on your front-end experience, you don’t need to try back-end languages ​​​​such as Java/PHP/golang, just use nodejs. You are also good at JavaScript, and node is also very powerful. If the api is simple, use node’s http module directly. If it is complex, use express http://expressjs.com/en/starter/installing.html
which can directly return json/jsonp objects

<code>res.json(null);
res.json({ user: 'tobi' });
res.status(500).json({ error: 'message' });</code>

I have never used Django, but I think Dj definitely supports returning json views. I simply searched for the keyword Django json view:

http://stackoverflow.com/questions/9262278/django-view-returning-json-without-using-template

It should satisfy what you want.

Said by @hsfzxjy

It is very simple to change the view return of Django. If you need to create a data interface, you can just write a Mixin and mix it in. You can take a look at this example: JsonResponseMixin

is to override the render_to_response method of Django class view and let it return json or other data. When using it, just write it like this:

<code class="python">    class TestView(JsonResponseMixin, DetailView):
        ...</code>

I’ve been getting into Lumen recently, you can try this elegant solution based on Laravel.

I think if you want to use Reactjs plus restful api, then the front-end display should not use MVC, but should be fluxjavascript - How to design a backend that only provides api?
My flux framework uses altjs, strictly follows the flux process, and The modular features of react are also well utilized, see: http://alt.js.org

As for restful api, it depends on your choice. There are various languages ​​and frameworks.

Just return json.
I now use restful of Yii2 to provide standard API for front-end access (IOS, ANDROID, WEB)

Don’t use template, just return json data via HttpResponse in views directly

Django only gives one div for react mount

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn