Last active
July 14, 2019 03:40
-
-
Save tbhaxor/ce735c07fd312e1605c79998fd3d8c30 to your computer and use it in GitHub Desktop.
Serving VUE Js Build directory using python and flask
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
before execution | |
1. build vue app - npm run build | |
2. install dependencies - pip install flask flask-cors | |
running the server | |
python serve.py | |
your site will be live on http://localhost:8000 | |
""" | |
from flask import Flask, url_for, redirect, send_file, request, Response | |
from flask_cors import CORS | |
app = Flask(__name__) | |
CORS(app) | |
@app.errorhandler(404) | |
def error(e): | |
if ( | |
"/js/" in request.base_url | |
and request.base_url.endswith(".js") | |
or "/css/" in request.base_url | |
and request.base_url.endswith(".css") | |
): | |
req = request.path | |
if req.endswith(".js"): | |
resp = Response(open("./dist/" + req[1:]).read()) | |
resp.headers["content-type"] = "application/javascript" | |
return resp | |
else: | |
resp = Response(open("./dist/" + req[1:]).read()) | |
resp.headers["content-type"] = "text/css; charset=utf-8" | |
return resp | |
return open("./dist/index.html", "r").read() | |
app.debug = True | |
app.run(port=8000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment