问题描述
我正在尝试使用flask/jinja从python中的google函数提供网页。
问题是谷歌函数似乎找不到我的静态文件(js/css),就好像我在我的 html 中合并了 css/js 那么它运行良好。
然后错误是:资源被解释为样式表但使用 MIME 类型 text/html 传输:http://MyGoogleCloudFunctionPath.net/static/styles/main.css
我的文件夹结构是:
应用程序
app.yaml
静态/js/reader.js
静态/css/main.css
模板/index.html
<link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='styles/main.css') }}">
<script type="text/javascript" src="{{ url_for('static',filename='js/reader.js') }}"></script>
我按照这里的建议添加了一个 Yaml [google 静态文件][1]
handlers:
- url: /static
static_dir: static
- url: /css
static_dir: static/styles
- url: /js
static_dir: static/js
此外,我正在使用 HTTP,因为我加载了来自其他站点的 http 资源。
最后,我认为 google 不是查看本地文件,而是尝试访问 www.googlefunctions/FOLDER_PATH/files
[1]:https://cloud.google.com/appengine/docs/standard/python/getting-started/serving-static-files
解决方法
等等,你有混合产品。首先,Cloud Functions 允许您只为一个端点提供后端服务(此处为 Python)。您无法从函数部署中提供静态文件
其次,您提到的 app.yaml 文件是 App Engine 配置文件,而不是 Cloud Functions。
就您而言,最简单的方法是部署在 App Engine 上,并且只使用一种产品(如果您想使用 Cloud Functions,我建议您也将 Cloud Storage 用于静态文件并在其中放置负载均衡器)两者的前面以相应地路由请求)