使用App Engine提供静态文件

Use*_*ser 19 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
Run Code Online (Sandbox Code Playgroud)

Cal*_*vin 37

这应该做你需要的:

https://gist.github.com/873098

说明:在App Engine Python中,可以将正则表达式用作URL处理程序app.yaml,并将所有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
Run Code Online (Sandbox Code Playgroud)

为了处理请求不具有识别的类型(结尾的URL .html,.png等等),或者/您需要重定向到这些请求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>
Run Code Online (Sandbox Code Playgroud)


Ed *_*all 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
Run Code Online (Sandbox Code Playgroud)


hyp*_*lug 8

如果您要映射/index.html:

handlers:
- url: /
  upload: folderpath/index.html
  static_files: folderpath/index.html
Run Code Online (Sandbox Code Playgroud)

url:将匹配的路径上,并支持正则表达式.

- url: /images
  static_dir: static_files/images
Run Code Online (Sandbox Code Playgroud)

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

<img src="/images/picture.jpg" />
Run Code Online (Sandbox Code Playgroud)

  • 小心,处理程序的顺序很重要! (2认同)