google-app-engine - 使用 App Engine 提供静态文件

标签 google-app-engine static-html

我创建了一个 App Engine 应用程序。到目前为止,我只有几个 HTML 文件要提供。每当有人访问 http://example.appengine.com/ 时,我该怎么做才能让 App Engine 提供 index.html 文件? ?

目前,我的 app.yaml 文件如下所示:

application: appname
version: 1
runtime: python
api_version: 1

handlers:

- url: /
  static_dir: static_files

最佳答案

这应该可以满足您的需要:

https://gist.github.com/873098

说明:在 App Engine Python 中,可以在 app.yaml 中使用正则表达式作为 URL 处理程序并将所有 URL 重定向到静态文件的层次结构。

示例 app.yaml :

application: your-app-name-here
version: 1
runtime: python
api_version: 1

handlers:
- url: /(.*\.css)
  mime_type: text/css
  static_files: static/\1
  upload: static/(.*\.css)

- url: /(.*\.html)
  mime_type: text/html
  static_files: static/\1
  upload: static/(.*\.html)

- url: /(.*\.js)
  mime_type: text/javascript
  static_files: static/\1
  upload: static/(.*\.js)

- url: /(.*\.txt)
  mime_type: text/plain
  static_files: static/\1
  upload: static/(.*\.txt)

- url: /(.*\.xml)
  mime_type: application/xml
  static_files: static/\1
  upload: static/(.*\.xml)

# image files
- url: /(.*\.(bmp|gif|ico|jpeg|jpg|png))
  static_files: static/\1
  upload: static/(.*\.(bmp|gif|ico|jpeg|jpg|png))

# index files
- url: /(.+)/
  static_files: static/\1/index.html
  upload: static/(.+)/index.html

# redirect to 'url + /index.html' url.
- url: /(.+)
  static_files: static/redirector.html
  upload: static/redirector.html

# site root
- url: /
  static_files: static/index.html
  upload: static/index.html

为了处理对不以可识别类型( .html.png 等)或 / 结尾的 URL 的请求您需要将这些请求重定向到 URL + /所以 index.html为该目录服务。我不知道在 app.yaml 中执行此操作的方法,所以我添加了一个 javascript 重定向器。这也可以通过一个微型 python 处理程序来完成。

redirector.html :

<!DOCTYPE html>
<html lang="en">
  <head>
    <script language="JavaScript">
      self.location=self.location + "/";
    </script>
  </head>
  <body>
  </body>
</html>

关于google-app-engine - 使用 App Engine 提供静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5609000/

相关文章:

python - 将 GqlQuery 结果集转换为 Python 字典

javascript - 使用 AJAX/JSON 提供静态 HTML 和生成内容有什么好处?

google-app-engine - appengine dispatch.yaml 没有找到我的模块?

python - GAE Python webapp2图片上传错误

json - 使用 POST 数据从 Node.js 提供静态文件

Tomcat 7.0.35 为静态 HTML 文件设置 HTTP 响应头 Content-Type 字符集

reactjs - 如何在静态index.html文件中使用react-router BrowserRouter

google-app-engine - 如何更改 dev_appserver 的日志记录级别

java - AppEngine 持久对象延迟?