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

th3g3ntl3man

我有这个文件夹树:

在此处输入图片说明

这是我的实际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进程结束,出现此错误:

第 0 步:应用程序检测失败:错误:node.js 检查器:“package.json”的“scripts”部分中的“start”和“server.js”文件均未找到。

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;
丹·科尼莱斯库

I see no way of specifying the location of the package.json and server.js files, neither in the 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).

The error you see could very well be caused by the deployment command not finding the mentioned files where it expects them.

In other words you'd have to move/copy the app.yaml file in the server directory. It might be possible to symlink the file to avoid actual code duplication if you want to also deploy from another directory (it works for the standard environment, but I'm not certain the same is true for the flexible one).

旁注:我认为您的静态处理程序(我怀疑这也可能是您寻求此类应用程序结构的原因)不会起作用 - 在app.yaml 参考Serving Static Files 中没有提及此类功能部分。也许您不小心查看了标准环境文档?(检查如何判断 Google App Engine 文档页面是否适用于第 1/2 代标准或灵活环境)如果您删除处理程序,则app.yaml文件中只剩下很少的内容 - 对所做的工作来说,重用价值相当低所以(如果可能的话)。我只是保持简单并坚持推荐的方式。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在不同于app.yaml的文件夹中上载Google App Engine(Go)项目

使用Google App Engine将文件放在不同的文件夹中... python

使用dispatch.yaml的Google App Engine路由

如何配置Google App Engine yaml文件以处理404错误

配置Google App Engine yaml文件以处理404错误

为什么子文件夹未部署到Google App Engine Node.js应用

Google App Engine的Yaml处理程序

在App Engine中为文件夹内的文件夹配置Yaml

Google App Engine Python:部署时yaml配置文件中出现错误

Google App Engine中的app.yaml配置

app.yaml文件:运行2个Python文件Google App Engine

如何在Google App Engine的app.yaml文件中配置servlet过滤器?

文件不希望被解析(app.yaml Google Cloud Platoform-App Engine上下文)

部署到Google App Engine

使用Google App Engine中的相同项目ID部署多个应用

如何在 Google App Engine 项目中引用 YAML 文件的相对路径?

yaml配置,用于向Google App Engine添加一个脚本文件

app.yaml文件的“ network:session_affinity:true”属性未反映在Google App Engine中

尝试通过app.yaml上传静态.mp3文件时,Google App Engine失败

使用Java SDK的Google App Engine项目部署错误

使用Maven在Google App Engine上部署SpringBoot / Angular 4

使用Luminus部署后,Google App Engine CSS无法加载

App Engine - app.yaml 交易图像

使用Python从Google App Engine上的外部URL上传文件

使用Go在Google App Engine中读取本地文件

如何使用Google App Engine上传多部分/表单文件?

Google App Engine可以识别并使用.htaccess文件吗?

如何使用python 3.5在Google App Engine中打开文件?

如何命名Google App Engine部署