我很難將create-react-app單頁應用程序整合到我的瓶子後端。我希望能夠從我的前端進行fetch/axios調用,如:axios.get('/ getResults')和fetch('/ getResults')。有些事情我已經嘗試但不限於將Flask端口指定爲3000,這與create-react-app使用的端口相同。另外,在create-react-app的「package.json」文件中使用了代理配置功能,但無濟於事。我懷疑我的文件夾結構和Flask代碼實現可能會導致這種情況。以下是我的文件夾結構和「app.py」代碼。我會得到任何幫助將不勝感激。如有必要,我可以提供更多信息。由於無法呈現來自Flask後端的create-react-app樣板視圖
項目
-build(包含靜態文件夾,index.html的...其他元文件)
-node_modules
-public
-src
app.py
的package.json
requirements.txt
app.py:
from flask import Flask, Response, request, jsonify, make_response, send_from_directory,render_template
app = Flask(__name__, static_path='/build/static/')
app.debug=True
@app.route('/')
def root():
print('Inside root function')
return app.send_static_file('index.html')
@app.route('/getResults', methods=["GET"])
def results():
print('Inside getResults path')
return app.send_static_file('index.html')
@app.route('/postData', methods=["POST"])
def data_results():
print('Inside postData path')
data = request.get_json
return jsonify(data)
@app.route('/<path:path>')
def send_js(path):
print("inside send_js fxn")
return send_from_directory('./build/static',path)
if __name__ == "__main__":
print("inside main host call")
app.run(host='0.0.0.0', port=3000)
錯誤,當我運行 「蟒蛇app.py」 我得到的是:
在終端:內部根函數 127.0.0.1 - - [12 /月/ 2017年9時42分24秒] 「GET/HTTP/1.1」 404 -
在瀏覽器:未找到 - 所請求的URL在服務器上找不到。如果您手動輸入網址,請檢查拼寫並重試。
你是如何運行create-react-app,用'npm start'? – azium
我運行它與紗線開始和紗線建立,所以我可以使用燒瓶後端的靜態文件 – dlvr
@dlvr你看我的回答https://stackoverflow.com/a/45245402/140837 – amirouche