node.js - 如何使用存储桶存储在 google flex/app engine 环境中提供静态文件?

标签 node.js google-app-engine gcloud

我有一个 nodejs 后端和一个 reactjs 前端。我正在使用 gcloud flex 环境(应用程序引擎)并希望使用 CDN 提供所有前端文件。我不希望请求触及我的 nodejs 服务器。我无法配置我的项目 app.yaml 来执行相同的操作。

我怀疑我的请求不是由 CDN 提供的,因为如果我在我的 nodejs 代码中注释以下行,我将无法再访问 index.html 。

app.use('/', express.static(path.resolve('./frontend/dist')));

下面是 YAML 文件。

handlers:
- url: /(.*\.html)
  mime_type: text/html 
  static_files: frontend/dist/\1 
  upload: frontend/dist/(.*\.html)

- url: /styles/(.*\.css) 
  mime_type: text/css 
  static_files: frontend/dist/styles/\1 
  upload: frontend/dist/styles/(.*\.css)

- url: /scripts/(.*\.js) 
  mime_type: text/javascript 
  static_files: frontend/dist/scripts/\1 
  upload: frontend/dist/scripts/(.*\.js)

- url: /images/(.*\.(bmp|gif|ico|jpeg|jpg|png)) 
  static_files: frontend/dist/images/\1 
  upload: frontend/dist/images/(.*\.(bmp|gif|ico|jpeg|jpg|png))

- url: / 
  static_files: frontend/dist/index.html 
  upload: frontend/dist/index.html

-  url: /.* 
  script: IGNORED
  secure: always

有没有一种方法可以配置应用引擎,使静态文件请求不响应我的 nodejs 后端服务器?

谢谢

最佳答案

你搞混了 standard GAE env app.yaml elements (静态内容配置)到你的 flex env app app.yaml .

在 flex 环境中提供静态内容是不同的。

你的 express.static用于提供静态文件的基于方法实际上对应于 Serving from your application :

Serving from your application

Most web frameworks include support for serving static files. In this sample, the application uses the express.static middleware to serve files from the ./public directory to the /static URL.

要在没有请求到达您的应用程序的情况下提供静态内容,您需要遵循 Serving from Cloud Storage :

Example of serving static files from a Cloud Storage bucket

This simple example creates a Cloud Storage bucket and uploads static assets using the Cloud SDK:

  1. Create a bucket. It's common, but not required, to name your bucket after your project ID. The bucket name must be globally unique.

    gsutil mb gs://<your-bucket-name>
    
  2. Set the ACL to grant read access to items in the bucket.

    gsutil defacl set public-read gs://<your-bucket-name>
    
  3. Upload items to the bucket. The rsync command is typically the fastest and easiest way to upload and update assets. You could also use cp.

    gsutil -m rsync -r ./static gs://<your-bucket-name>/static
    

You can now access your static assets via https://storage.googleapis.com/<your-bucket-name>/static/....

For more details on how to use Cloud Storage to serve static assets, including how to serve from a custom domain name, refer to How to Host a Static Website.

For more information on how to use the Cloud Storage API to dynamically upload, download, and manipulate files from within your application, see Using Cloud Storage.

关于node.js - 如何使用存储桶存储在 google flex/app engine 环境中提供静态文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41631414/

相关文章:

使用 gcloud 的负载均衡器 SSL 证书

authentication - Gcloud auth 登录保存到 legacy_credentials 文件夹

node.js - 在 Jade 包含中使用变量

python - 使用 POST 在 Google 应用引擎中传递数据

google-app-engine - Jetty 无法在 appengine flexible 中启动 spring boot 应用程序

java - 找出哪个客户端正在进行 API 调用 : Google app engine

google-app-engine - Google Cloud SDK 0.9.57 版本中的 gcloud 中断了应用程序的部署

javascript - Sequelize 和 Sequelize 层次结构 : How do I execute an accessor in a model getter?

javascript - 无效的主机头 Heroku Node.js React 应用程序

javascript - 无法使用具有 X509 用户的 Mongoose 连接到 Mongo DB