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

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

谢谢!

最佳答案

为了运行您展示的示例,请按照 instructions in the docs 操作:

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

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

  • 将代码复制到 sample.js 文件中并运行 node example.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
    

    关于node.js - 错误 : (gcloud. app.deploy)错误响应:[9] 应用程序启动错误:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51110955/

    相关文章:

    java - Google App Engine 调度程序不会将请求路由到所有可用实例

    node.js - 为同一路线 sails 多个处理程序

    javascript - 转换对象结构数组

    node.js multer 模块无法处理错误

    spring-boot - 为 Google Cloud Run 外部化应用程序配置

    MySQL lower_case_table_names = 1 和 Kubernetes yml 文件,MySQL 服务器启动错误

    node.js - 如何保护 Redis 和 Socket.IO 实时服务器,以便只有经过身份验证的用户才能收听?

    javascript - 对数组的 mongoose.model 进行索引返回未定义

    linux - 如何为 gcloud 的 linux 服务器分配外部 ip?

    kubernetes - 使用kubeadm创建的k8s集群上的Pod到Pod通讯问题