Node.js + Google App Engine提供静态内容服务

4
谷歌云文档对我使用的Node.js应用程序的app.yaml文件的可用语法不是很明确。我使用了 GAE Python文档中描述的语法,从中找到了handlers机制。
    handlers:
    - url: /index.html
      static_files: /public/index.html
      upload: /public/index.html

我已经避开了我的expressjs规则,以提供/public/index.html内容,最终出现了404错误,这意味着GAE没有将我的页面作为静态内容提供:
    $ curl -i "http://my-project/index.html"
    HTTP/1.1 404 Not Found
    ...

您对此有任何线索吗?Node.js适用于制作API、生成动态内容...但我更喜欢使用Google后端甚至Nginx服务器来处理静态内容。

更新

去掉前导斜杠并没有解决问题。我稍微更改了app.yaml配置:

    handlers:
    - url: /api/.*
      secure: always
      script: app.js
    - url: /.*
      secure: always
      static_dir: public

我仍然在/index.html上遇到404未找到错误,并且在调用/api/stuff时获得正确的200 OK响应。

这是我的项目目录结构:

    Project root
    |- app.js
    |- app.yaml
    |- package.json
    |- public/
    |  `-- css/
    |     `-- style.css
    |  `-- js/
    |     `-- main.js
    |  `-- index.html

你是否仍然在使用Nodejs运行时的app.yaml中遇到static_dir静态文件处理程序的问题?丹提供的答案是否有效(因为从评论中似乎不太清楚)? - Nicholas
1个回答

1

文档页面上的示例通常足够使用。

static_filesupload值中,您有一个前导/,它应该只是相对于您的应用程序目录的路径。

还可能有其他原因,起点将是您的应用程序日志,无论是在开发服务器上还是在已部署的GAE上。

更新:

根据静态目录处理程序文档

A static directory example:

handlers:
# All URLs beginning with /stylesheets are treated as paths to static files in
# the stylesheets/ directory.
- url: /stylesheets
  static_dir: stylesheets

url

A URL prefix. This value uses regular expression syntax (and so regexp special characters must be escaped), but it should not contain groupings. All URLs that begin with this prefix are handled by this handler, using the portion of the URL after the prefix as part of the file path.

根据这个引用,我怀疑 app.yaml 的处理程序规范中 url 中的通配符可能会导致问题(例如,/index.html 可能会被 static_dir 解析逻辑扩展为 /index.html/),我会将 url 替换为明确指示目录的形式,如下所示:

- url: /
  secure: always
  static_dir: public

我不太喜欢将应用程序命名空间的顶层 / 与静态目录绑定,但对于通常是静态的应用程序可能还可以接受。确保您始终将此处理程序规范放在您的 app.yaml 文件中的最后,以避免问题。

谢谢你的回答,我去掉了前面的“/”,但是仍然没有改变。我遇到了这个错误,意味着处理程序已经被解析,但是文件仍然无法找到:“Static file referenced by handler not found: public/index.html”。 - Clément Désiles
请编辑问题,添加您的项目目录结构,以显示 index.html 文件所在的位置。 - Dan Cornilescu
我已经使用一个“/”设置了您的处理程序,但仍然无法正常工作。 我仍然收到错误消息:“处理程序引用的静态文件未找到:public /”,“处理程序引用的静态文件未找到:public / index.html”和“处理程序引用的静态文件未找到:public / index.html /”,分别在请求“/”,“/ index.html”,“/ index.html /”时出现。 你有什么线索吗? 你知道如何向Google开发人员报告错误吗? 谢谢! - Clément Désiles
你仍然将URL设置为/.*,而不是/ - Dan Cornilescu
不是这样的。我在我的处理程序中使用了 /:
  • url: / secure: always static_dir: public
- Clément Désiles
昨晚我的怀疑得到了证实,请看这个问答:http://stackoverflow.com/questions/33447890/static-files-are-missing/33448239#33448239 - Dan Cornilescu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接