【问题标题】:ERROR: (gcloud.app.deploy) Error Response: [9] Application startup error:错误:(gcloud.app.deploy)错误响应:[9]应用程序启动错误:
【发布时间】:2018-06-30 01:10:27
【问题描述】:

当我运行 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 上部署我的应用服务器。感谢您的帮助。

谢谢!

【问题讨论】:

  • 问题解决了吗?

标签: node.js google-cloud-platform


【解决方案1】:

为了运行您展示的示例,请关注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)的函数中

然后我像在sample nodejs app 中一样将express.js 添加到应用程序中,然后我调用了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

【讨论】:

    猜你喜欢
    • 2019-03-27
    • 2019-06-16
    • 2017-01-14
    • 2018-02-14
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 2021-06-29
    相关资源
    最近更新 更多