node.js 讀取yaml文件

node.js 讀取yaml文件

1、安裝yamljs包,具體的安轉命令爲:sudo npm inastall -g yamljs

2、編寫名爲'manifest.yml'的yaml配置文件

Config:

  Srvc: 8008

  IPAddress: 12.0.0.1

3、讀取配置文件代碼:

具體做法是將配置文件中的代碼轉換爲json

YAML = require('yamljs');

// Load yaml file using YAML.load

nativeObject = YAML.load('manifest.yml');

 

jsonstr = JSON.stringify(nativeObject);

jsonTemp = JSON.parse(jsonstr, null);

console.log(jsonTemp)

console.log(jsonstr);

console.log(jsonTemp.Config.Srvc);

 

yamljs參考資料:

https://www.npmjs.com/package/yamljs

Parse yaml string:

nativeObject = YAML.parse(yamlString);

Dump native object into yaml string:

yamlString = YAML.stringify(nativeObject[, inline /* @integer depth to start using inline notation at */[, spaces /* @integer number of spaces to use for indentation */] ]);

Load yaml file:

nativeObject = YAML.load('file.yml');

Load yaml file:

YAML.load('file.yml', function(result)
{
    nativeObject = result;
});
YAML = require('yamljs');
 
// parse YAML string
nativeObject = YAML.parse(yamlString);
 
// Generate YAML
yamlString = YAML.stringify(nativeObject, 4);
 
// Load yaml file using YAML.load
nativeObject = YAML.load('myfile.yml');

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章