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

标签 node.js google-app-engine google-cloud-platform httpserver app.yaml

Google Cloud 文档对于用于我的 Node.js 应用程序的 app.yaml 文件的可用语法不是很精确。

我使用了 Python documentation for GAE 中描述的语法,从中我找到了处理程序机制:

    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 Not found,并且得到 调用 /api/stuff 时正确的 200 OK 应答。

这是我的项目树结构:

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

最佳答案

文档页面上的示例通常就足够了。

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

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

更新:

根据Static directory handlers doc :

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

我不喜欢将应用程序命名空间的顶级 / 绑定(bind)到静态目录,但对于一般静态的应用程序来说可能没问题。确保始终将此处理程序规范保留在 app.yaml 文件中以避免出现问题。

关于Node.js + Google App Engine 提供的静态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33300320/

相关文章:

javascript - 如何将特定时区的 UTC 日期转换为 UTC +0(默认格林威治)?

javascript - 如何在 if 语句所在的 for 循环之外返回 if 语句的 else

python - 获取索引错误: string index out of range while loading file in bigquery

google-cloud-platform - 定期自动执行 Colab 笔记本

python - 在无限循环中渲染

cloud - 如何从我的本地计算机访问 GCP 的 VM 实例?

javascript - Node/Javascript 将对象属性设置为数组不起作用?

javascript - 我可以在安装端从 NPM 包中排除文件吗?

android - 从 Blobstore 获得 500

google-app-engine - 是否可以在 google app engine (go) 中使用用户本地网络浏览器时区来格式化日期?