如何在AppEngine Standard和Node.js中提供静态文件 [英] How to serve static files in AppEngine Standard and nodejs

查看:116
本文介绍了如何在AppEngine Standard和Node.js中提供静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档说,您必须更新您的app.yaml-就像使用AppEngine中的任何语言一样。稍后还将指出,对于本地开发,您可能还希望服务器也响应静态请求。但是,当我将非常简单的app.yaml更新为:

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

似乎所有请求仍然最终进入我的脚本-该示例会在prod实例中返回404,因为这些文件不会上传。我可以强制将它们上传,然后我的nodejs服务器响应静态请求-但是我认为这个app.yaml的想法是对其进行配置,以便在我的应用程序逻辑之外提供静态文件?

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?

推荐答案

要明确-可以在生产Nodejs Standard AppEngine 中托管静态文件而无需需要使用JS服务器。但是,对于本地开发,您必须找到一种在计算机上运行时在本地提供这些文件的方法。出于这个原因,您将处理程序放置在Express中,以处理静态文件,该文件在生产中永远不可触及-因为app.yaml处理程序是第一遍。

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.

如果您想确定Express.js没有在生产环境中提供静态文件,则可以执行以下操作:

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'));
}

这篇关于如何在AppEngine Standard和Node.js中提供静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆