Node.js + Google App Engine提供的静态内容 [英] Node.js + static content served by Google App Engine
问题描述
Google云文档对用于我的 Node.js 应用程序的 app.yaml 文件的可用语法不太精确。
我使用 Python中描述的语法GAE 的文档,我从中找到了处理程序 mecanism:
处理程序:
- url:/index.html
static_files:/public/index.html
上传:/public/index.html
我避免使用expressjs规则来服务 /public/index.html 内容,最后我得到了404错误,这意味着GAE是不作为静态内容提供我的网页:
$ curl -ihttp://my-project/index.html
HTTP / 1.1 404未找到
...
你有没有关于这个的线索? Node.js与制作API,生成动态内容相关...但我更喜欢使用Google后端甚至是Nginx服务器来处理静态内容。
更新 h2>
删除引导斜杠并未解决问题。我稍微改变了我的 app.yaml 配置:
处理程序:
- url:/ api /.*
secure:总是
script:app.js
- url:/.*
secure:总是
static_dir:public
code>
而且我仍然在 这是我的项目树结构: /index.html上找到 404 Not found code>,当调用
/ api / stuff
时获得
正确的 200 OK 答案。
$ b
pre $ $ $ $ c $项目根
- app。 js
| - app.yaml
| - package.json
| - public /
| ` - css /
| ` - style.css
| ` - js /
| ` - main.js
| ` - index.html
在$ static_files 中有一个前导
/
code>和 upload
值,它们应该只是您应用目录顶部的相对路径。
还可能有其他原因,起点是您的应用程序的日志,无论是在您的开发服务器上,还是在已部署的GAE上。
更新:
根据静态目录处理程序文档:
静态目录示例:
处理程序:
#以/ stylesheets开头的所有URL都被视为
#stylesheets /目录中静态文件的路径。
- url:/ stylesheets
static_dir:stylesheets
url
一个URL前缀。该值使用正则表达式语法(因此必须转义正则表达式
特殊字符),但不应包含
分组。所有以此前缀开头的URL均由此
处理程序处理,并将前缀之后的部分URL作为
文件路径的一部分。
基于此引用,我怀疑 app.yaml $
可能实际上将扩展为 url中的通配符
c $ c>的处理程序规范可能会导致问题(例如, /index.html/
由 static_dir
解析逻辑),我会替换url来清楚地指出一个目录,如下所示: p>
- url:/
安全:总是
static_dir:public
我不喜欢将应用程序名称空间的顶层 /
绑定到静态目录,但它可能是一个普遍的静态应用程序。确保您始终将此处理程序规范保留在 app.yaml
文件中以避免发生问题。
The Google Cloud documentation isn't very precise on the available syntax for the app.yaml file used for my Node.js application.
I used the syntax described in the Python documentation for GAE, from which I've found the handlers mecanism:
handlers:
- url: /index.html
static_files: /public/index.html
upload: /public/index.html
I've avoid my expressjs rule to serve the /public/index.html content and finally I got a 404 error, meaning GAE is not serving my page as a static content:
$ curl -i "http://my-project/index.html"
HTTP/1.1 404 Not Found
...
Do you have any clue on this? Node.js is relevant for making APIs, generating dynamic content... But I prefer using the Google backends or even a Nginx server to handle static contents.
Update
Removing the leading slashes didn't fixed the issue. I slightly changed my app.yaml configuration:
handlers:
- url: /api/.*
secure: always
script: app.js
- url: /.*
secure: always
static_dir: public
And I still get 404 Not found on /index.html
, and getting
the correct 200 OK answser when calling /api/stuff
.
Here is my project tree structure:
Project root
|- app.js
|- app.yaml
|- package.json
|- public/
| `-- css/
| `-- style.css
| `-- js/
| `-- main.js
| `-- index.html
The examples at the very documentation page should normally suffice.
You have a leading /
in the static_files
and upload
values, which should be just relative paths to the top of your app dir.
There could be other reasons as well, the starting point would be the logs for your app, either on your development server or on GAE if already deployed.
Update:
According to the 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.
Based on this quote I'd suspect the wildcards in the url
of the app.yaml
's handler spec may be causing issues (for example the /index.html
might actually be expanded to /index.html/
by the static_dir
parsing logic), I'd replace the url to clearly indicate a directory, like this:
- url: /
secure: always
static_dir: public
I'm not a fan of tying the top level /
of the app's namespace to a static dir, but it may be OK for a generally static app. Make sure you always keep this handler spec last in your app.yaml
file to avoid issues.
这篇关于Node.js + Google App Engine提供的静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!