错误:(gcloud.app.deploy)错误响应:[9]应用程序启动错误: [英] ERROR: (gcloud.app.deploy) Error Response: [9] Application startup error:

查看:85
本文介绍了错误:(gcloud.app.deploy)错误响应:[9]应用程序启动错误:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行gcloud app deploy时,我得到错误响应9.我收到的错误消息是

When I run gcloud app deploy I get error response 9. The error message I get is

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

Please help me, I am trying to learn deploying my app server on GCP. Appreciate your help.

谢谢!

推荐答案

为运行您显示的示例,请遵循文档中的说明:

In order to run the example you show, please follow the instructions in the docs:

  1. 下载凭据JSON文件并创建一个指向该文件的环境变量:
  1. Download the credentials JSON file and create an environment variable that points to this file:

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

  1. 将代码复制到sample.js文件中,然后运行node sample.js.
  1. Copy the code in, i.e., sample.js file and run node sample.js.

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

On the other hand, if you want to deploy a nodejs app, you'll need express.js or any other framework.

我在express.js的帮助下完成了以下操作:

I did the following with the help of express.js:

我将您的代码包装在称为saveEntity(datastore)

I wrapped your code in a function called saveEntity(datastore)

然后,我像在 express.js 添加到应用中.google.com/appengine/docs/flexible/nodejs/quickstart"rel =" nofollow noreferrer>示例nodejs应用,然后我从那里调用saveEntity(datastore):

Then I added express.js to the app like in the sample nodejs app and from there I called 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"
    }
}

另外,您应该知道已弃用:

runtime: nodejs
vm: true

您可以改为这样做:

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

这篇关于错误:(gcloud.app.deploy)错误响应:[9]应用程序启动错误:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆