Javascript-how to design a backend that only provides APIs?

Source: Internet
Author: User
For example, if the front-end uses a react or vue framework and I only want to use ajax to obtain the corresponding data in the background, what should I do? Or how to choose the backend? I have used Django to write something similar before, but I still don't feel I can leave the view provided by Django. For example, if the front-end uses a react or vue framework and I only want to use ajax to obtain the corresponding data in the background, what should I do? Or how to choose the backend? I have used Django to write something similar before, but I still don't feel I can leave the view provided by Django.

Reply content:

For example, if the front-end uses a react or vue framework and I only want to use ajax to obtain the corresponding data in the background, what should I do? Or how to choose the backend? I have used Django to write something similar before, but I still don't feel I can leave the view provided by Django.

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

1. First, the selection of background languages is not important. The key is what you are familiar with and what language you are familiar with. it is faster to get started.
2. what you need to do is to separate the front and back ends. The backend provides APIs. the API returns the data format required by the front-end. the front-end obtains data through APIs. Render the page by yourself at the front end.
3. because I am using PHP, I suggest using PHP in the background. MVC separation is used in the background. Because only APIs are provided in the background, V can be omitted. First, we recommend that you select a framework that supports configurable routing, then M operates the database, obtains data from the background, and C converts the data into json format and transmits it to the foreground. You need to configure routes according to the framework rules, and then write the Controller code and Model code. The frameworks include ThinkPHP and Laravel.

Based on your front-end experience, you don't have to try back-end languages such as Java, PHP, and golang. you can use nodejs directly. JavaScript is also good at, and node is also very powerful. If simple APIs direct node http Module, if complicated, use express http://expressjs.com/en/starter/installing.html
Json/jsonp objects can be directly returned.

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

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

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

It should satisfy your needs.

@ Hsfzxjy

It is very easy to change Django's view and return. if you need to write a Mixin through the data interface and then mix it in, you can look at this example: JsonResponseMixin

That is, rewrite the Django class view.render_to_responseTo return json or other data. You can write it like this:

    class TestView(JsonResponseMixin, DetailView):        ...

You have recently entered the Lumen trap. you can try this elegant solution based on Laravel.

I think you should use Reactjs and restful api, so you should not use MVC for front-end presentation, but flux
My flux framework uses altjs, strictly following the flux process, and The react modular feature is also well played out, see: http://alt.js.org

The restful api depends on your choice. There are various languages and frameworks.

Return json.
In my words, restful of Yii2 is now used to provide standard APIs for front-end access (IOS, ANDROID, and WEB)

Don't use a template. Is it okay to directly return json data from HttpResponse in views?

Django only sends react mount to one p

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.