1

我正在开发一个带有 AngularJS 前端 + GAE 后端(Python 和 Flask)的应用程序。我在设置 app.yaml 以路由使用 Flask-Restless 扩展创建的 API 端点时遇到了麻烦。我的 app.yaml 文件如下所示:

application: myAppID
version: 1
runtime: python27
threadsafe: true
api_version: 1

handlers:
# handler 1
- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

# handler 2
- url: /api/.*
  script: main.app

# handler 3
- url: /test
  script: main.app

# handler 4
- url: (.*)/
  static_files: app\1/index.html
  upload: app #this is the frontend folder for Angular

# handler 5
- url: (.*)
  static_files: app\1
  upload: app #this is the frontend folder for Angular

在 Angular 中,路由配置如下所示:

App.config(['$stateProvider', '$locationProvider', '$urlRouterProvider', 'RouteHelpersProvider',
function ($stateProvider, $locationProvider, $urlRouterProvider, helper) {
  'use strict';

  $locationProvider.html5Mode(false);

  // default route
  $urlRouterProvider.otherwise('/app/dashboard');

  // other routes ...
}]);

main.py 文件如下所示:

from flask import Flask
import os
from werkzeug import debug
from flask import jsonify
from google.appengine.ext.webapp.util import run_wsgi_app

app = Flask('myApp')

if os.getenv('SERVER_SOFTWARE') and os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/'):
    app.debug = False
else:
    app.debug = True

if app.debug:
    app.wsgi_app = debug.DebuggedApplication(app.wsgi_app, True)

@app.route('/test')
def test():
    return jsonify(test={"json": "test"})

import models

run_wsgi_app(app)

models是包含 Flask-SQLAlchemy 模型和 Flask-Restless 端点的文件。

Angular 部分正确加载,例如此 URL 工作正常:

  • A) http://localhost:8080/#/app/dashboard

但是 GAE 后端部分对这些 URL 的响应是 500 错误:

  • B) http://localhost:8080/api/person
  • C) http://localhost:8080/test

如果我删除handler 4然后和URL 工作正常handler 5,但 Angular 前端停止工作。BC

我做错了什么?

4

1 回答 1

1

我在旅途中,所以用我的手机写东西不是那么有趣......

无论如何,我在我的应用程序中所做的是我只有一个触发烧瓶应用程序的处理程序。

在烧瓶应用程序中, / 路由通常会将 Angular Web 应用程序作为静态文件返回。

您需要配置您的 Flask 应用程序,它会知道静态(HTML、JS 等)文件夹。

编辑:

app.yaml 应如下所示:

handlers:
- url: .*  # This regex directs all routes to main.app
   script: main.app

main.app 是烧瓶应用程序.. 现在让我们看看如何从路线“/”提供角度应用程序

from flask import Flask, render_template
app = Flask(__name__, static_folder='/templates') # This sets /templates to be the folder for all JS HTML CSS files

@app.route('/')
def wellcomePage():
    return app.send_static_file('index.html')

app.js 文件中的角度路由配置:

app.config(['$routeProvider', '$locationProvider',
function($routeProvider, $locationProvider) {
    $routeProvider
        .when('/', {
            templateUrl: 'templates/views/home.html'
        }).... Some More Routes..

注意 templateUrl: 'templates/...'

请看看我的应用程序。我想它会帮助你理解我在这里想说的......

github 上的 SE 中心

当我拿到一个该死的键盘时,我会编辑这个答案:)

让我知道这是否有帮助。

于 2015-07-03T00:29:25.103 回答