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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months 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)

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The 2-Hour Python Plan: A Realistic Approach The 2-Hour Python Plan: A Realistic Approach Apr 11, 2025 am 12:04 AM

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

PHP: Is It Dying or Simply Adapting? PHP: Is It Dying or Simply Adapting? Apr 11, 2025 am 12:13 AM

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

How to start the server with redis How to start the server with redis Apr 10, 2025 pm 08:12 PM

The steps to start a Redis server include: Install Redis according to the operating system. Start the Redis service via redis-server (Linux/macOS) or redis-server.exe (Windows). Use the redis-cli ping (Linux/macOS) or redis-cli.exe ping (Windows) command to check the service status. Use a Redis client, such as redis-cli, Python, or Node.js, to access the server.

Vue.js in the Frontend: Real-World Applications and Examples Vue.js in the Frontend: Real-World Applications and Examples Apr 11, 2025 am 12:12 AM

Vue.js is a progressive JavaScript framework suitable for building complex user interfaces. 1) Its core concepts include responsive data, componentization and virtual DOM. 2) In practical applications, it can be demonstrated by building Todo applications and integrating VueRouter. 3) When debugging, it is recommended to use VueDevtools and console.log. 4) Performance optimization can be achieved through v-if/v-show, list rendering optimization, asynchronous loading of components, etc.

How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

H5: Tools, Frameworks, and Best Practices H5: Tools, Frameworks, and Best Practices Apr 11, 2025 am 12:11 AM

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.

See all articles