Google App Engine - 使用相同的 app.yaml 部署不同的文件夹

Google App Engine - Deploy different folder with the same app.yaml

我有这个文件夹树:

这是我的实际 app.yaml:

runtime: nodejs
env: flex

manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10

handlers:
  - url: /api/.*
    static_files: server/server.js
    upload: server/server.js
  - url: /
    static_files: www/build/index.html
    upload: www/build/index.html
  - url: /
    static_dir: www/build

但总是,当我尝试使用以下命令部署应用程序时:gcloud app deploy --stop-previous-version 进程结束时出现此错误:

Step #0: Application detection failed: Error: node.js checker: Neither "start" in the "scripts" section of "package.json" nor the "server.js" file were found.

server/package.json是:

{
  "name": "server",
  "version": "1.5.2",
  "engines": {
    "node": "13.x"
  },
  "main": "server.js",
  "description": "ConstaFAST server",
  "scripts": {
    "start": "node server.js"
  },
  "license": "MIT",
  "dependencies": {
    "@hapi/joi": "^16.1.7",
    "base64-img": "^1.0.4",
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.19.0",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "generate-password": "^1.4.2",
    "jsonwebtoken": "^8.5.1",
    "mongoose": "^5.8.4",
    "nodemailer": "^6.4.2",
    "pdfkit": "^0.11.0",
    "qrcode": "^1.4.4"
  },
  "devDependencies": {
    "morgan": "^1.9.1"
  }
}

www/package.json 是:

{
  "name": "www",
  "version": "0.1.6",
  "private": true,
  "engines": {
    "node": "13.x"
  },
  "dependencies": {
    "@material-ui/core": "^4.8.2",
    "@material-ui/icons": "^4.5.1",
    "bootstrap": "^4.4.1",
    "formik": "^2.1.1",
    "jspdf": "^1.5.3",
    "qrcode": "^1.4.4",
    "qrcode.react": "^1.0.0",
    "react": "^16.11.0",
    "react-bootstrap": "^1.0.0-beta.14",
    "react-dom": "^16.11.0",
    "react-router": "^5.1.2",
    "react-router-dom": "^5.1.2",
    "react-scripts": "3.3.0",
    "react-stripe-elements": "^6.0.1",
    "yup": "^0.28.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

最后,server/server.js的内容是:

const express = require('express');
const app = express();
const path = require('path');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
require('dotenv').config()

const agencyRoutes = require('./api/routes/agency');
const companyRoutes = require('./api/routes/company');
const userRoutes = require('./api/routes/user');
const qrcodeRoutes = require('./api/routes/qrcode');
const vehicleRoutes = require('./api/routes/vehicle');
const writerRoutes = require('./api/routes/writer');

const port = process.env.PORT || 8080;

mongoose.connect(String(process.env.DB_CONNECT), {
  useCreateIndex: true,
  useNewUrlParser: true,
  useUnifiedTopology: true
}, () => console.log('Connect to the database'));

mongoose.Promise = global.Promise;

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use( (req, res, next) => {
  res.header('Access-Control-Allow-Origin', 'https://constafast.cf');
  res.header(
    'Access-Control-Allow-Headers',
    'Origin, X-Requested-With, Content-Type, Accept, Authorization'
    );
  res.header('Connection', 'Keep-Alive');

  if ( req.method === 'OPTIONS' ) {
    res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE, GET');
    return res.status(200).json({});
  }

  next();
});

app.use(express.static(path.resolve(__dirname, '../www', 'build')));

app.use('/api/agency', agencyRoutes);
app.use('/api/company', companyRoutes);
app.use('/api/user', userRoutes);
app.use('/api/qrcode', qrcodeRoutes);
app.use('/api/vehicle', vehicleRoutes);
app.use('/api/writer', writerRoutes);

app.get('*', (req, res) => {
  res.sendFile(path.resolve(__dirname, '../www', 'build', 'index.html'));
});

app.use( (req, res, next) => {
  const error = new Error('Not found');
  error.status = 404;
  next(error);
});

app.use( (error, req, res, next) => {
  res.status(error.status || 500);
  res.json({
    error: {
      message: error.message
    }
  });
});

app.listen(port, () => console.log(`Server up and running on port ${port}`));
module.exports = app;

我无法指定 package.jsonserver.js 文件的位置,在 app.yaml reference, nor in the gcloud app deploy reference, so I'd guess it has to be located side-by-side with the app.yaml file (as seen in the sample app).

您看到的错误很可能是由于部署命令没有在预期的位置找到提到的文件造成的。

换句话说,您必须 move/copy app.yaml 目录中的 app.yaml 文件。 可能 可以对文件进行符号链接以避免实际的代码重复,如果你还想从另一个目录部署(它适用于标准环境,但我不确定是否相同对于灵活的)。

旁注:我认为您的静态处理程序(我怀疑这也可能是您寻求此类应用程序结构的原因)将无法正常工作 - app.yaml reference or in the Serving Static Files section. Maybe you accidentally looked at the standard environment docs? (check 中未提及此类功能) 如果您删除处理程序,app.yaml 文件中所剩无几 - 这样做的努力的重用价值相当低(如果可能的话)。我只想让事情变得简单并坚持推荐的方式。