【发布时间】:2014-03-10 04:00:22
【问题描述】:
我有一个使用 Google App Engine (GAE) 运行的单页应用 (SPA)。 GAE 做了三件事:
- 提供 index.html 文件
- 提供静态文件(JS、CSS 等)
- 通过 REST 提供动态文件(图像、文本等)
我使用下面的 app.yaml 配置。
handlers:
- url: /app
static_dir: app
- url: /.*
script: main.app
我的理解是,这应该与进入 /app 文件夹的任何请求相匹配,该文件夹将为我的静态文件提供服务。然后所有的 REST 服务和主索引页面将被 /.* 捕获并由 main.py 处理
但是,我看到以下行为:
- 如果我删除 /app 处理程序,我可以成功地提供 index.html(通过 Jinja 模板)和 REST 服务(例如 localhost/subjects/)。 但是,我看不到静态文件(如预期的那样)。
- 如果我添加 /app 处理程序,则 index.html 文件不会提供服务并给出“内部服务器错误”IOError(errno.EACCES, 'file not access', filename)。 不过,当我请求静态文件如:“localhost/app/app.js”时,就成功了。
这里有什么我遗漏的吗?我不明白为什么两者会冲突。
谢谢!
注意事项: Google App Engine 1.8.9,Python 2.7,本地开发
编辑:
这是我用来提供页面的 Python 代码
path = os.path.join(os.path.dirname(__file__), 'app')
jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(path))
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/html'
template_values = {}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
我的目录结构如下:
/
main.py
app.yaml 等
-
应用程序
index.html
app.js
模块 A
moduleA.tpl.html
moduleA.js
编辑 2:
我将 index.html 移到根目录(/),然后使用以下 python 代码:
path = os.path.dirname(__file__)
jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(path))
看起来(非常有趣)index.html 到 Jinja 模板和静态目录文件的“双重映射”导致了一个问题。我想知道这样做的最佳实践方法是什么。我使用 Jinja 的原因之一:将(GAE 生成的)登录/注销链接添加到 index.html 文件。除此之外,没有理由使用它。
【问题讨论】:
-
你把 index.html 放在哪里了?似乎您将 index.html 放在导致问题的 /app 中。
-
@marcadian:感谢您的指点。我确实将 index.html 放在 /app 中,但我认为这不是问题所在。如果您看到我编辑的问题,我让 Jinja 指向 /app 文件夹,然后请求它为 index.html 提供服务。我觉得问题更多在于静态文件配置。