【问题标题】:How to get customAPI url from node.js app?如何从 node.js 应用程序获取 customAPI url?
【发布时间】:2015-05-06 00:14:31
【问题描述】:

我刚开始使用 BlueMix,在我的空间中我有:

  • 云集成服务:使用基本安全连接,我为此创建了一个 API 端点;然后在该云集成服务中,我通过导入一个 swagger 1.2 文件添加了相应的 API,并将该 customAPI 发布到我的组织;
  • 一个非常简单的 node.js 应用程序;

从云集成服务> API 视图中,我可以获得不同资源的 URL(例如 http://endpoint_ip:endpoint_port/api/version/path_to_resource),因此我可以在我的 node.js 应用程序中对这些 URL 进行硬编码并且它可以工作。

但是,如果我将云集成服务甚至 customAPI 绑定到我的 node.js 应用程序,我不会在 VCAP_SERVICES 中获得有关端点 URL 的任何信息;但我已经看到了 API URL 可用的 VCAP_SERVICES 示例。

下面是我的 VCAP_SERVICES

{"CloudIntegration": [
      {
         "name": "Cloud Integration-b9",
         "label": "CloudIntegration",
         "plan": "cloudintegrationplan",
         "credentials": {
            "userid": "apiuser@CloudIntegration",
            "password": "S!2w3e40",
            "apis": [
               {
                  "name": "Catalog Manager API",
                  "desc": "Catalog Manager API",
                  "resource": ""
               }
            ]
         }
      }
   ]
} 



我想要实现的是避免在我的应用程序中对 URL 进行硬编码,因为我可以将 BlueMix 服务绑定到它,并且可能从环境中获取信息。
难道我做错了什么?或者这不是它应该工作的方式?
我也不明白为什么 VCAP_SERVICES.CloudIntegration[0].credentials.apis[0].resource 中没有任何内容,即使我有我的 customAPI 指定资源。

【问题讨论】:

  • 您是否重新启动了您的应用程序?如果不重新启动或重新暂存,VCAP_SERVICES 更改将不会反映。
  • @Jack-Junjie Cai:是的,我重新启动了应用程序。事实上,每次我添加/绑定新服务时,它都会要求我重新启动/重新暂存应用程序

标签: integration ibm-cloud


【解决方案1】:

@瑞克

确保在配置云集成服务后“发布”您的 API。然后服务凭据将反映更改:

"CloudIntegration": [
  {
     "name": "Cloud Integration-v5",
     "label": "CloudIntegration",
     "plan": "cloudintegrationplan",
     "credentials": {
        "userid": "apiuser@CloudIntegration",
        "password": "S!2w3e40",
        "apis": [
           {
              "name": "SwaggerPetStore",
              "desc": "SwaggerPetStore",
              "resource": "http",
              "baseurl": "http://mypypatchank.mybluemix.net"
           }
        ]
     }
  }
]

同理,如果你使用API​​管理服务,就会有一个对应的VCAP_SERVICES入口

"Swagger Petstore v1 : Sandbox 551b2dcf0cf2521d98d061d4 prod": [
  {
     "name": "Swagger Petstore v1 : Sandbox prod-w0",
     "label": "Swagger Petstore v1 : Sandbox 551b2dcf0cf2521d98d061d4 prod",
     "plan": "plan1 : Sandbox prod",
     "credentials": {
        "clientID": "55cfe3fa-ff59-474c-a1b6-46d3cc9871",
        "clientSecret": "uK3xM3eF4cA1qF7yW8mC2lP6wS6aG7sQ5cL2yJ4sC6iS1dE7",
        "url": "https://api.eu.apim.ibmcloud.com/garciatemx1ibmcom/sb/api"
     }
  }
]

【讨论】:

  • 是的,我已经发布了我的 API,但是我的资源/baseurl 没有改变(仍然是空的)。难道是我的招摇文件缺少一些信息?还是我必须通过 API 管理才能完成这项工作?
【解决方案2】:

因为您的目标是“避免在我的应用程序中对 URL 进行硬编码,因为我可以将 BlueMix 服务绑定到它,并可能从环境中获取信息。”我想建议使用用户提供的服务。

这将创建一个用户提供的服务并启动交互式输入,让您输入 api url 和密码。如果需要,您可以添加更多参数。

cf cups servicename -p "url, password"

将此服务绑定到您的应用程序并重新暂存。您可以使用cfenv 模块在您的 Node.js 应用程序中轻松访问这些参数。

var cfenv = require("cfenv");
var appEnv = cfenv.getAppEnv();
var myService = appEnv.getService("servicename");

//use myService.credentials.url to access the url value.
//use myService.credentials.password to access the password value. 

用户提供的服务 VCAP_SERVICES 如下所示:

{
  "user-provided": [
    {
      "name": "servicename",
      "label": "user-provided",
      "credentials": {
        "url": "myURL",
        "password": "myPassword"
      }
    }
  ]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 2015-12-22
    • 1970-01-01
    相关资源
    最近更新 更多