错误:(gcloud.app.deploy)错误响应:[9]应用程序启动错误:

时间:2018-06-30 01:10:28

标签: node.js google-cloud-platform

我运行gcloud app deploy时收到错误响应9。收到的错误消息是

Updating service [default] (this may take several minutes)...failed.                                                                                                                
ERROR: (gcloud.app.deploy) Error Response: [9] 
Application startup error:

app.js

// Imports the Google Cloud client library
const Datastore = require('@google-cloud/datastore');

// Your Google Cloud Platform project ID
const projectId = 'myid';

// Creates a client
const datastore = new Datastore({
    projectId: projectId,
});

// The kind for the new entity
const kind = 'Task';
// The name/ID for the new entity
const name = 'sampletask1';
// The Cloud Datastore key for the new entity
const taskKey = datastore.key([kind, name]);

// Prepares the new entity
const task = {
    key: taskKey,
    data: {
        description: 'Buy milk',
    },
};

// Saves the entity
datastore
    .save(task)
    .then(() => {
        console.log(`Saved ${task.key.name}: ${task.data.description}`);
    })
    .catch(err => {
        console.error('ERROR:', err);
    });

package.json

{
  "name": "first-application",
  "version": "1.0.0",
  "description": "First program using cloud datastore",
  "main": "app.js",
  "scripts": {
    "start":"node app.js",
    "deploy":"gcloud app deploy",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Ragav",
  "license": "ISC",
  "dependencies": {
    "@google-cloud/datastore": "^1.4.1"
  }
}

app.yaml

runtime: nodejs
vm: true

请帮助我,我正在尝试学习在GCP上部署我的应用服务器。感谢您的帮助。

谢谢!

1 个答案:

答案 0 :(得分:0)

要运行您显示的示例,请遵循instructions in the docs

  1. 下载凭证JSON文件并创建一个指向该文件的环境变量:

export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/credential-file.json"

  1. 将代码复制到sample.js文件中,然后运行node sample.js

另一方面,如果要部署nodejs应用,则需要express.js或任何其他框架。

我在express.js的帮助下做了以下事情:

我将您的代码包装在名为saveEntity(datastore)的函数中

然后,我像在express.js中一样,将sample nodejs app添加到应用中,然后从那里我叫saveEntity(datastore):

app.get('/', (req, res) => {
    saveEntity(datastore);
    res.status(200).send("Entity saved").end();
}); 

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

module.exports = app;

完整的结果是这样:

// Imports the Google Cloud client library
const Datastore = require('@google-cloud/datastore');
//Needed to create the node app
const express = require('express')

const app = express();

// Your Google Cloud Platform project ID
const projectId = 'my-project-id';

// Creates a client
const datastore = new Datastore({
    projectId: projectId,
});

function saveEntity(datastore){
    // The kind for the new entity
    const kind = 'JulyTask';
    // The name/ID for the new entity
    const name = 'sampletask1';
    // The Cloud Datastore key for the new entity
    const taskKey = datastore.key([kind, name]);

    // Creates entity data
    const task = {
        name: 'Learn something',
        status: 'In progress',
        description: 'Friday 6 July'
    }

    //With the data and the key, create the entity
    const entity = {
        key: taskKey,
        data: task
    }

    // Saves the entity
    datastore
        .upsert(entity)
        .then(() => {
            console.log('Saved:\n' + JSON.stringify(entity));
            return true;
        })
        .catch(err => {
            console.error('ERROR:', err);
            return false;
        });
}

app.get('/', (req, res) => {
    saveEntity(datastore);
    res.status(200).send("Entity saved").end();
}); 

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

module.exports = app;

package.json

{
    "name": "first-application",
    "version": "1.0.0",
    "description": "First program using cloud datastore",
    "main": "app.js",
    "scripts": {
        "start": "node app.js",
        "deploy": "gcloud app deploy",
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "author": "Ragav",
    "license": "ISC",
    "dependencies": {
        "@google-cloud/datastore": "^1.4.1",
        "express": "^4.16.3"
    }
}

另外,您应该知道这是deprecated

runtime: nodejs
vm: true

您可以改为:

runtime: nodejs
env: flex
service: my-service-name