【问题标题】:Send response back to client after google compute engine api call in node js在节点 js 中调用谷歌计算引擎 api 后将响应发送回客户端
【发布时间】:2018-03-06 23:53:22
【问题描述】:

我正在尝试使用 google cloud Kubernetes API for node js 从谷歌云获取 Kubernetes 集群详细信息。

以下是我在谷歌文档中找到的示例。

var google = require('googleapis');
var container = google.container('v1');

authorize(function(authClient) {
  var request = {
     projectId: 'my-project-id',  
     zone: 'my-zone',  
     clusterId: 'my-cluster-id',  
     auth: authClient,
  };

  container.projects.zones.clusters.get(request, function(err, response){
    if (err) {
       console.error(err);
    return;
  }

// TODO: Change code below to process the `response` object and send the detail back to client.

  console.log(JSON.stringify(response, null, 2));
  });
});

function authorize(callback) {
   google.auth.getApplicationDefault(function(err, authClient) {
     if (err) {
       console.error('authentication failed: ', err);
       return;
   }
   if (authClient.createScopedRequired && authClient.createScopedRequired()) {
       var scopes = ['https://www.googleapis.com/auth/cloud-platform'];
       authClient = authClient.createScoped(scopes);
   }
   callback(authClient);
  });
}

由于 google get API 是异步函数,如何将 API 的响应返回给客户端。

【问题讨论】:

    标签: node.js google-cloud-platform google-compute-engine kubernetes-container


    【解决方案1】:

    问题是您想对数据做什么?这不是 API 是否异步的问题,这段代码的 sn-p 应该在控制台中返回与此请求相同的 JSON:

    GET https://container.googleapis.com/v1beta1/projects/[PROJECT ID]/locations/[ZONE]/clusters/[CLUSTER NAME]
    

    函数和回调应该注意它是异步的。

    【讨论】:

      猜你喜欢
      • 2016-04-27
      • 1970-01-01
      • 2018-05-24
      • 2016-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多