2

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?

yathern
  • 319
  • 1
  • 3
  • 14
  • Can you show the development server log for one such request? – Dan Cornilescu Jun 16 '18 at 14:45
  • @DanCornilescu - The logs simply say "No handlers matched this URL." for a request to /index.html or /. The actual HTTP response shows in the headers that it was handled by Express - the node.js webserver. Instead, it shouldn't go to Express at all, and should be handled by whatever AppEngine uses to host static files. That would be my assumption at least - but the docs aren't too clear on how this happens - and all the GitHub examples also host static files through Express - since that must be done for local dev. – yathern Jun 16 '18 at 15:42
  • "those files won't be uploaded", what do you mean by that? – Steren Jun 22 '18 at 17:02

3 Answers3

3

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'));
}
yathern
  • 319
  • 1
  • 3
  • 14
1

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.

Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
  • Thanks for the answer - as an update - if I remove the Express server entirely, I can serve files using static_files it appears - which serves using Google's Frontend servers. Static_dir doesn't seem to work, but I'll fiddle around with it a bit more. EDIT: Figured it out! Turns out that static_dir doesn't work the way I thought it would with wildcards. Simply give it '/' to catch all requests. Which makes sense. – yathern Jun 16 '18 at 15:51
0

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.

Steren
  • 7,311
  • 3
  • 31
  • 51