3

我正在尝试将这个angular python 项目和这个restful flask 项目结合起来。

Directory

- /app
    - css/app.css
    - js/app.js
    - index.html
- app.yaml
- main.py
- appengine_config.py
- vendor.py
- requirements.txt

app.yaml

application: your-application-id-here
version: 1
runtime: python37
api_version: 1
threadsafe: yes

handlers:
- url: /rest/.*
  script: main.APP

- url: /(.+)
  static_files: app/\1
  upload: app/.*

- url: /
  static_files: app/index.html
  upload: app/index.html

main.py

from flask import Flask
from flask.ext import restful


APP = Flask(__name__)
api = restful.Api(APP)


class HelloWorld(restful.Resource):
    def get(self):
        return {'hello': 'world'}


api.add_resource(HelloWorld, '/rest/query/')


@app.errorhandler(404)
def page_not_found(e):
    """Return a custom 404 error."""
    return 'Sorry, Nothing at this URL.', 404


@app.errorhandler(500)
def page_not_found(e):
    """Return a custom 500 error."""
    return 'Sorry, unexpected error: {}'.format(e), 500

该文件夹中的所有内容都app/与 python angular 项目完全相同。

如果我从 中注释掉以下内容app.yaml,我可以访问/rest/query并获得预期的输出。

- url: /(.+)
  static_files: app/\1
  upload: app/.*

- url: /
  static_files: app/index.html
  upload: app/index.html

但是,当它没有被注释掉时,我得到一个404for /rest/query。在/我能够看到index.html加载有角钩的静态页面。app.js由于无法查询,因此未填充数据/rest/query

如何设置使用 Angular 的 GAE Flask 宁静项目?

4

2 回答 2

1

我必须这样做的方式是部署一个带有数据库的烧瓶 API 作为 Web 服务和独立的应用程序。

接下来,您必须添加 CORS 以允许您的服务与另一个应用程序通信,在本例中是使用 Angular 框架的前端客户端。

然后我部署我的 Angular 应用程序,它将接受对后端 Web 服务的 api 调用。

我的项目将 sql server 和 .net core web api 用于部署到 azure 的 Web 服务器。以及一个部署到谷歌云的 Angular 应用程序。我还有另一个部署到天蓝色的角度应用程序。

我正在为我的谷歌云角度前端应用程序开发一个新的 api 和 dB,以使测试和开发变得更容易。

于 2019-06-20T16:07:50.323 回答
1

我不是这种解决方法的忠实拥护者,但我将路由从发生移动app.yamlmain.py

main.py

from flask import Flask, render_template
from flask.ext import restful


class HelloWorld(restful.Resource):
    def get(self):
        return {'hello': 'world'}


app = Flask(__name__)
api = restful.Api(app)
api.add_resource(HelloWorld, '/rest/query')


@app.route('/')
def root():
    return render_template('index.html')


@app.errorhandler(404)
def page_not_found(e):
    """Return a custom 404 error."""
    return 'Sorry, Nothing at this URL.', 404


@app.errorhandler(500)
def page_not_found(e):
    """Return a custom 500 error."""
    return 'Sorry, unexpected error: {}'.format(e), 500

app.yaml

application: application-id-here
version: 1
runtime: python37
api_version: 1
threadsafe: yes

handlers:
- url: /static
  static_dir: static

- url: /.*
  script: main.app

directory

- /static
    - css/app.css
    - js/app.js
    - partials/...
- /templates/index.html
- app.yaml
- main.py
- appengine_config.py
- vendor.py
- requirements.txt
于 2019-06-20T18:10:52.697 回答