'Flask serve react app build without interfering with Flask-socket.io requests
Im attempting to serve a react app via flask using the catch-all found in the top answer of this question, which works great for serving all of the build files, it also lets my normal API routes function as intended.
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def serve(path):
if path != "" and os.path.exists('spotiguess/build' + '/' + path):
return send_from_directory(app.static_folder, path)
else:
return send_from_directory(app.static_folder, 'index.html')
However, the problem is my socket.io connection is being caught by this catch-all and served the index.html
text. (from the else clause)
I'm using Flask-Socket.io and my Socket.io requests are being sent to the root of my domain, which worked just fine before I implemented the catch-all to serve the build.
I'm wondering what I can do to serve the static files of my build folder, my normal flask routes for API, and also maintain Flask-Socket.io
's ability to catch requests with the @socketio.event
decorator.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|