Я бы хотел обработать всю маршрутизацию на стороне клиента с двумя исключениями:
Сервер должен обрабатывать любой маршрут, который начинается с / api и обслуживает файлы из /dist.
Вот моя структура папок:
./
├── Client
│ ├── dist
│ │ └── bundle.js
│ │ └── bundle.css
│ ├── index.html
│ ├── node_modules
│ ├── package.json
│ ├── src
│ │ ├── __tests__
│ │ ├── components
│ │ └── index.tsx
│ ├── tsconfig.json
│ ├── tslint.json
│ ├── webpack.config.js
│ └── yarn.lock
├── README.md
├── app.py
└── requirements.txt
app.py
from flask import Flask, jsonify, render_template
app = Flask(__name__, template_folder="Client")
@app.route('/api/random')
def random_name():
response = {
'randomName': choice(["Ben", "Joe", "Robert", "Amy"])
}
return jsonify(response)
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def index(path):
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
Как я могу подать bundle.js
из index.html
?Я пытался использовать url_for
в index.html
, но это не работает - я получаю 404.