Table of Contents
Reply content:
Home Backend Development PHP Tutorial javascript - How to design a backend that only provides api?

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

Jul 06, 2016 pm 01:53 PM
javascript php python react.js vue.js

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>
Copy after login

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:

    class TestView(JsonResponseMixin, DetailView):
        ...
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
Python vs. JavaScript: Development Environments and Tools Python vs. JavaScript: Development Environments and Tools Apr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Python vs. C  : Understanding the Key Differences Python vs. C : Understanding the Key Differences Apr 21, 2025 am 12:18 AM

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Golang vs. Python: The Pros and Cons Golang vs. Python: The Pros and Cons Apr 21, 2025 am 12:17 AM

Golangisidealforbuildingscalablesystemsduetoitsefficiencyandconcurrency,whilePythonexcelsinquickscriptinganddataanalysisduetoitssimplicityandvastecosystem.Golang'sdesignencouragesclean,readablecodeanditsgoroutinesenableefficientconcurrentoperations,t

Laravel vs. Python (with Frameworks): A Comparative Analysis Laravel vs. Python (with Frameworks): A Comparative Analysis Apr 21, 2025 am 12:15 AM

Laravel is suitable for projects that teams are familiar with PHP and require rich features, while Python frameworks depend on project requirements. 1.Laravel provides elegant syntax and rich features, suitable for projects that require rapid development and flexibility. 2. Django is suitable for complex applications because of its "battery inclusion" concept. 3.Flask is suitable for fast prototypes and small projects, providing great flexibility.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

What happens if session_start() is called multiple times? What happens if session_start() is called multiple times? Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Python vs. C  : Which Language to Choose for Your Project? Python vs. C : Which Language to Choose for Your Project? Apr 21, 2025 am 12:17 AM

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

Python vs. JavaScript: Use Cases and Applications Compared Python vs. JavaScript: Use Cases and Applications Compared Apr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

See all articles