簡體   English   中英

Reactjs create-react-app my-app 后端集成

[英]Reactjs create-react-app my-app backend integration

我使用 create-react-app 創建了一個基本的應用程序。 當我運行我的后端服務器(在 python 中使用flask)時,我提供默認的index.html文件:

from flask import Flask, render_template
app = Flask(__name__, static_folder='../web/public')

@app.route("/")
def index():
    return render_template('../web/public', 'index.html')

if __name__ == '__main__':
    app.run()

這是 create-react-app 生成的 index.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="theme-color" content="#000000">

    <link rel="manifest" href="%PUBLIC_URL%/manifest.json">
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">

  </head>
  <body>
    <noscript>
      You need to enable JavaScript to run this app.
    </noscript>
    <div id="root">
    </div>
  </body>
</html>

當將此文件提供給瀏覽器時,它顯然什么都不渲染,只有在從 create-react-app 生成的文件夾運行 npm start 時才會渲染 React 應用程序。 如何僅使用 Flask 后端服務器為實際應用提供服務?

我嘗試鏈接 index.js 腳本:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';

ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();

但是瀏覽器輸出了Uncaught SyntaxError: Unexpected identifier就行import React from 'react';

基本上,我的問題是如何僅使用 Flask 服務器為 React 應用程序提供服務?

要在生產中使用通過 create-react-app 創建的應用程序,請運行以下命令:

npm run build

這將創建一個可以部署到服務器的 JavaScript 包。

查看文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM