简体   繁体   English

为什么 Flask 会与 Reactjs 一起用于 Restful API?

[英]Why would Flask be used with Reactjs for Restful APIs?

I have heard of people who use Flask with Reactjs to build REST APIs.我听说有人使用FlaskReactjs来构建 REST API。 However, I am very confused by why Flask is needed when Reactjs can handle GET and POST requests .然而,当Reactjs 可以处理 GET 和 POST 请求时,我对为什么需要 Flask 感到非常困惑。

To bring some context behind why this question is important for me: At work, we build web apps where a customer/user inputs some data (eg favourite food).提供一些背景说明为什么这个问题对我很重要:在工作中,我们构建网络应用程序,客户/用户输入一些数据(例如最喜欢的食物)。 This data is then processed using python libraries and scripts and then an output (eg recipe for the food) is returned to the user.然后使用 python 库和脚本处理这些数据,然后将输出(例如食物的配方)返回给用户。

Why might Flask with React be better than React by itself?为什么 Flask with React 比 React 本身更好?

React and flask are two separate solutions for different problems. React 和 flask 是针对不同问题的两种不同的解决方案。 In the context of a REST API, flask (on top of python) can be used to provide the API.在 REST API 的上下文中,flask(在 Python 之上)可用于提供API。 React on the other hand is a client-side framework and is thus at best indirectly involved in consuming it.反应在另一方面是一个客户端框架,并且因此最好在间接参与消费它。

Neither React nor flask is needed, both are independent convenience frameworks.不需要 React 和 Flask,两者都是独立的便利框架。 React for the JavaScript client-side, flask for the Python server-side. React 用于 JavaScript 客户端,flask 用于 Python 服务器端。

React makes it easier to provide an HTML interface to the user, flask makes it easier to reply to incoming HTTP requests on the server. React 可以更轻松地向用户提供 HTML 界面,flask 可以更轻松地回复服务器上传入的 HTTP 请求。

React is just a javascript library which helps in building user interfaces, whereas flask is a microframework for building web applications. React 只是一个有助于构建用户界面的 javascript 库,而 Flask 是用于构建 Web 应用程序的微框架。 Handling http requests in the context of React means that it can make http requests and make decisions based on what they output.在 React 上下文中处理 http 请求意味着它可以发出 http 请求并根据它们的输出做出决定。 React does not have control over the business logic ie the way the requests are going to get processed, which in your case is deciding recipe based on user's input. React 无法控制业务逻辑,即处理请求的方式,在您的情况下,这是根据用户输入决定配方。 That's where flask or a backend comes into picture, it would take the input given by the frontend, do some processing and return some output.这就是flask或后端出现的地方,它会接受前端给出的输入,做一些处理并返回一些输出。 Over here http is just a medium of passing information from frontend to the backend.在这里,http 只是一种将信息从前端传递到后端的媒介。

To understand these concepts a bit more you could research more on the MVC architecture.要更多地理解这些概念,您可以对 MVC 架构进行更多研究。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM