简体   繁体   中英

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

I have this folder tree:

在此处输入图片说明

This is my actual 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

But always, when I try to deploy the app with the following command: gcloud app deploy --stop-previous-version the process end whit this error:

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.

The server/package.json is:

{
  "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"
  }
}

and the www/package.json is:

{
  "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"
    ]
  }
}

And at the end, the content of server/server.js is:

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).

Side note: I don't think your static handlers (which I suspect could also be the reason for which you seek such app structure) will be working - there's no mention of such capability in the app.yaml reference or in the Serving Static Files section. Maybe you accidentally looked at the standard environment docs? (check How to tell if a Google App Engine documentation page applies to the 1st/2nd generation standard or the flexible environment ) If you drop the handlers there's very little left in the app.yaml file - a rather low re-use value for the effort to do so (if even possible). I'd just keep things simple and stick to the recommended way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM