Node.js

設定ファイルを jsonやyamlで記述する。

$ npm install config yaml
$ cat config/default.yaml
adb:
  mac_address:'xx:xx:xx:xx:xx:xx'
  bot_url: '/xxxxxxxxx/xxxxxxxxx/xxxxxxxxxxxxxxxxxxxxxxxx'
$ cat index.js
var config = require ('config');
var adb = config.adb;
console.log(adb);
console.log(adb.mac_address);
console.log(adb.bot_url);

jsonの場合は

$ cat config/default.json
{
"adb": {
  "mac_address": "xx:xx:xx:xx:xx:xx",
  "bot_url": "/xxxxxxxxx/xxxxxxxxx/xxxxxxxxxxxxxxxxxxxxxxxx"
 }
}

でおなじ設定値が得られる。

この設定情報は、環境設定で渡すことも可能で、

$ export NODE_CONFIG='{"adb":{...上記のJSONを改行なくしたモノ..}'

ってやればOK。実行時に設定を渡したいばあいなど。


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS