javascript - How to design a backend that only provides api?
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:
class TestView(JsonResponseMixin, DetailView): ...
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 flux
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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 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.

Golangisidealforbuildingscalablesystemsduetoitsefficiencyandconcurrency,whilePythonexcelsinquickscriptinganddataanalysisduetoitssimplicityandvastecosystem.Golang'sdesignencouragesclean,readablecodeanditsgoroutinesenableefficientconcurrentoperations,t

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.

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.

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.

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 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.
