部署到Google App Engine

问题描述

我的app.yaml配置文件是:

runtime: nodejs12


handlers:
  # Serve all static files with url ending with a file extension
  - url: /(.*\..+)$
    static_files: build/\1
    upload: build/(.*\..+)$
  # Catch all handler to index.html
  - url: /.*
    static_files: build/index.html
    upload: build/index.html
    secure: always
    redirect_http_response_code: 301
  - url: /static
    static_dir: static

当我部署时,它不会更新前端。我认为每次部署时静态文件都不会更改。它在缓存吗?我不知道该怎么办

解决方法

首先,您要部署到新版本并且不将流量迁移到该版本吗?在GCP控制台中,您可以转到“ App Engine”>“版本”(https://console.cloud.google.com/appengine/versions)查看当前部署的版本,它会告诉您哪个正在接收流量。

接下来,请确保您的文件已实际部署。如果您在GCP控制台(https://console.cloud.google.com/debug)中转到“调试器”,则可以浏览已部署的文件。如果您有多个版本,则有一个版本下拉菜单可在它们之间切换,因此请确保您浏览的版本正确。

正在缓存吗?

如果没有另外指定,App Engine会将静态资产的缓存周期设置为10分钟。

default_expiration可选。为应用程序的所有静态文件处理程序设置全局默认缓存周期。您还可以为特定的静态文件处理程序配置缓存持续时间。该值是一串数字和单位,用空格分隔,其中单位可以是d表示天,h表示小时,m表示分钟,s表示秒。例如,“ 4d 5h”将缓存过期设置为首次请求文件后的4天零5小时。如果省略,则生产服务器将到期时间设置为10分钟。

https://cloud.google.com/appengine/docs/standard/nodejs/config/appref#runtime_and_app_elements

编辑:同样,您的handlers:的顺序也很重要。它们按顺序检查。因此,url: /.*的规则可能是捕获url: /static的规则打算捕获的所有流量

此外,我认为您所有的url: /.*处理程序返回index.html是一个错误。最好有类似url: /index.html的东西返回index.html,让其余的只有404。您可能还有其他错误/输入错误,您现在没有注意到。

编辑2:

我实际上使您当前的设置奏效感到惊讶,因为在app.yaml的参考中它说:

要使用静态处理程序,您的处理程序中至少有一个必须包含行script: auto或定义一个entrypoint元素才能成功部署。

https://cloud.google.com/appengine/docs/standard/nodejs/config/appref

所以我整理了一个示例项目,这是我的项目结构:

- build
  - index.html
- node_modules
  - <folders-from-npm-install>
- static
  - css
    - bootstrap.css
- app.js
- app.yaml
- package.json

app.yaml中,我做了几件事。

  • 我之所以把url: /static放在第一位是因为url: /(.*\..+)$正在捕获/static/css/bootstrap.css
  • 我删除了index.html的条目,因为url: /(.*\..+)$已经在照顾它
  • 我添加了最后一个全部捕获条目,以将所有剩余流量发送到app.js

app.yaml:

runtime: nodejs12

handlers:
  - url: /static
    static_dir: static
  # Serve all static files with url ending with a file extension
  - url: /(.*\..+)$
    static_files: build/\1
    upload: build/(.*\..+)$
  - url: /.*
    script: auto

对于app.jspackage.json,我在这里https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/appengine/hello-world/standard复制了它们来自GAE的“ Hello World”示例

app.js:

'use strict';

// [START gae_node_request_example]
const express = require('express');

const app = express();

app.get('/',(req,res) => {
  res.status(200).send('Hello,world!').end();
});

// Start the server
const PORT = process.env.PORT || 8080;
app.listen(PORT,() => {
  console.log(`App listening on port ${PORT}`);
  console.log('Press Ctrl+C to quit.');
});
// [END gae_node_request_example]

module.exports = app;

package.json:

{
  "name": "appengine-hello-world","description": "Simple Hello World Node.js sample for Google App Engine Standard Environment.","version": "0.0.2","private": true,"license": "Apache-2.0","author": "Google Inc.","repository": {
    "type": "git","url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
  },"engines": {
    "node": ">=12.0.0"
  },"scripts": {
    "start": "node app.js","test": "mocha --exit test/*.test.js"
  },"dependencies": {
    "express": "^4.17.1"
  },"devDependencies": {
    "mocha": "^8.1.3","supertest": "^5.0.0"
  }
}

我按照问候世界中的说明运行了npm installnpm start在本地运行,但是不幸的是,这并没有模仿handlers:app.yaml的行为>

当我部署它时,去https://my_project_id.appspot.com/index.html确实可行。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...