1

AngularJS フロントエンド + GAE バックエンド (Python と Flask) を使用してアプリを開発しています。Flask-Restless エクステンションで作成した API エンドポイントをルーティングするための app.yaml の設定に問題があります。私の 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)

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

を削除すると URL は正常に機能しhandler 4ますhandler 5が、Angular フロントエンドは機能しなくBなります。C

私が間違っていることは何ですか?

4

1 に答える 1

1

外出先なので、携帯からの書き込みはあまり楽しくありません...

とにかく、私のアプリで行ったことは、フラスコアプリをトリガーするハンドラーが 1 つしかないことです。

フラスコ アプリでは、通常 / ルートは角度のある Web アプリを静的ファイルとして返します。

静的 (HTML、JS など) フォルダーを認識できるように、Flask アプリを構成する必要があります。

編集:

app.yaml は次のようになります。

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

main.app はフラスコ アプリです。次に、ルート '/' から angular アプリを提供する方法を見てみましょう

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 に答える