使用App Engine提供静态文件

时间:2011-04-10 00:47:15

标签: 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

5 个答案:

答案 0 :(得分:37)

这应该做你需要的:

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等)或/结尾的网址的请求,您需要将这些请求重定向到{{ 1}}这样就可以提供该目录的URL + /。我不知道在index.html中有这样做的方法,所以我添加了一个javascript重定向器。这也可以通过一个小的python处理程序来完成。

app.yaml

redirector.html

答案 1 :(得分:9)

可以使用(app.yaml)完成:

handlers:
- url: /appurl
  script: myapp.app

- url: /(.+)
  static_files: staticdir/\1
  upload: staticdir/(.*)

- url: /
  static_files: staticdir/index.html
  upload: staticdir/index.html

答案 2 :(得分:8)

如果您要将/映射到index.html

handlers:
- url: /
  upload: folderpath/index.html
  static_files: folderpath/index.html

url:将在路径上匹配并支持正则表达式。

- url: /images
  static_dir: static_files/images

因此,如果您的图片文件存储在static_files/images/picture.jpg,请使用此处:

<img src="/images/picture.jpg" />

答案 3 :(得分:1)

在WEB-INF / web.xml中输入:

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

答案 4 :(得分:0)

这是app.yaml,我是如何获得Jekyll生成的网站的:

Superhuman