分离紧密耦合的前端和后端

Segregate tightly coupled frontend and backend

Python-2.7, Web Framework : Flask

My frontend and backend are present on the same server. I am using flask web framework( GET/POST APIs and also render templates). Now I want to segregate them and host them in two different machines. Since the code base is tightly coupled and I am mostly rendering HTML files, How can I achieve this ?

如果您直接从您的服务器呈现 html,您仍然可以分散您的前端和后端。使用 REST API 或类似的东西创建后端服务器。然后,对于您的前端服务器,只需让它调用后端服务器,然后 return 这些结果在 html 中呈现给客户端。这样,如果您不想,就不需要公开后端服务器。

这是一个例子:

后端

@app.route('/mydata/')
def mydata:
    # ... logic/service calls ...
    return jsonify(result)

前端

import requests

@app.route('/')
def index():
    api_result = requests.get(API_URL + '/mydata/')
    # ... Map api_result to result ...
    return render_template('index.html', result=result)