Help us understand the problem. What is going on with this article?

Bluemixのnode.jsでホスト名を取得する

More than 3 years have passed since last update.

Bluemixのアプリで自分のアプリのホスト名(xxx.mybluemix.net)※を取得しようとして、ちょっと手間取ってしまったのでメモ。
※別名:「経路」とか「Application Route」とか

VCAP_APP_HOSTがあるじゃないか -> ×

と思って叩いたら、戻り値が"0.0.0.0"でした。firewallの内側だし、実行しているインスタンスにインターネット上のホスト名(FQDN)を割り振られているわけではないですよと。

console.log("process.env.VCAP_APP_HOST=" + process.env.VCAP_APP_HOST );//->0.0.0.0
console.log("process.env.VCAP_APP_PORT=" + process.env.VCAP_APP_PORT );//->61076

ちなみに同じようにNode.jsのosモジュール使っても取れませんでした。11桁くらいの文字列(例.1941uvtfexx)が返ってきます。

var os = require('os');
var hostname = os.hostname();
console.log("os_hostname=" + hostname );//->1941uvtfexx

VCAP_APPLICATIONを使う -> ○

Bluemixの環境変数にはダッシュボードのGUIから確認できるVCAP_SERVICESの他に、VCAP_APPLICATIONてのもありまして、この中の'application_uris'にありそうでした。

VCAP_APPLICATION: '{"limits":{"mem":128,"disk":1024,"fds":16384},"application_version":"ffcaf4eb-14f4-4efc-8ef7-279c02765303","application_name":"xxx-mfs","application_uris":["xxx-mfs.mybluemix.net"],"version":"ffcaf4eb-14f4-4efc-8ef7-279c02765303","name":"xxx-mfs","space_name":"dev","space_id":"ae9ac0c1-240e-4391-b104-ad667a014xxx","uris":["xxx-mfs.mybluemix.net"],"users":null,"application_id":"dbfc0e91-8e87-4a0c-b169-920d22c5xxxx","instance_id":"a34ea5fd8b63405e9df603d4e2fxxxx","instance_index":0,"host":"0.0.0.0","port":62106,"started_at":"2015-11-04 09:24:17 +0000","started_at_timestamp":1446629057,"start":"2015-11-04 09:24:17 +0000","state_timestamp":1446629057}'

ので、例えば↓のような感じでとりあえずは良いのかなと。

var applicationRoute;
if (process.env.VCAP_APPLICATION ){
    var vcapApplications = JSON.parse(process.env.VCAP_APPLICATION);
    applicationRoute = vcapApplications.application_uris[0];
    console.log("applicationRoute=" + applicationRoute);
}else{
    applicationRoute= "xxx.mybluemix.net";//ローカル実行時とかはこっちで
};

参考.BluemixのLiberty Javaの場合

同じく「VCAP_APPLICATION」を使うか、「HttpServletRequest.getServerName()」など幾つかの方法でで取れるらしいです。

How to get the real request (route) hostname in a liberty web app?
https://developer.ibm.com/answers/questions/163490/how-to-get-the-real-request-route-hostname-in-a-li/?smartspace=bluemix

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away