How to serve static files in AppEngine Standard and nodejs

て烟熏妆下的殇ゞ 提交于 2019-12-24 00:45:34

问题


The documentation says that you simply must update your app.yaml - like you would for any language within AppEngine. It also states later on that for local development, you probably want your server to respond to static requests as well. However, when I update my very simple app.yaml to be as such:

runtime: nodejs8

handlers:
  - url: /apiEndPoint
    script: auto

  - url: /.*
    static_dir: public

It seems all requests still end up coming in to my script - which will return a 404 in the prod instance, since those files won't be uploaded. I can force them to be uploaded, and then my nodejs server responds to the static requests - but I thought the idea of this app.yaml was to configure it so static files are served outside of my app logic?


回答1:


So to be clear - you can host static files in production Nodejs Standard AppEngine without needing to use a JS server. However, for local development, you must find a way to serve those files locally when running on your machine. For this reason, you put the handler in Express, for static files, which should never be touched in production - since the app.yaml handler is the first-pass.

If you want to be positive Express.js is not serving up static files in production, you can do so by doing something like this:

// Production instances automatically have this environment variable.
const isLocal = (process.env.NODE_ENV !== "production");

if(isLocal) {
  app.use(express.static('public'));
}



回答2:


The static files are uploaded during deployment, but not in the same place as the app's code. They're uploaded in Google's infra dedicated for directly serving static content. This could be confirmed by increasing the log verbosity of the deployment command.

When a request URL matches one of the static handlers it should be directed to this dedicated infra, it shouldn't reach your app code. It should be relatively easy to confirm with an actual deployment.

As for the local development, I'm not exactly sure how the Node.Js server behaves (indeed, the docs appear to suggest Express may be needed to handle static files), but the Python one serves itself the static files based solely on the app.yaml static handler configs, without hitting any of the app code. Could be because of the still very new Node.JS standard environment support.




回答3:


The static files that you want to serve need to be deployed along your application code with gcloud app deploy.

Your app.yaml file says:

  • Any request that matches /apiEndPoint will be routed to your Node.js app
  • Any other request URL will serve a static file from your public folder and not arrive to your application (once deployed).

For example: /index.html will serve public/index.html if this file has not been deployed, then it will return a 404 page.



来源:https://stackoverflow.com/questions/50888860/how-to-serve-static-files-in-appengine-standard-and-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!