get cfg from a json object or from json cfg files, which either in relative directories or absoluted directories

Usage no npm install needed!

<script type="module">
  import getcfg from '';


require modules


function errArray2StrArray: convert error array to string array, to easily print the array

function errlist2strlist(errArray){

function getcfg: search each path, find the first cfg file, and return its json cfg as a pure object. If none exists, return the default cfg. If error, return null.

parameters: defCfgInfo: should have keys 'version', 'cfg', and 'filename' jsonCfgFileName: e.g. myservice.js.json, which should have the keys above searchPaths: array of paths, e.g. ['.', '..', '../..', '/home'], default is ['.'] baseDir: the base dir of a relative searching path, default is '.' callback function: rtnErrList: err lists rtnCfg: null means error found //TODO function getcfg(defCfgInfo, jsonCfgFileName, searchPaths, baseDir, callback) { ### handle default values ### update search paths to absolute diretory ### return if error found already return rtnCfg; ### seach the paths, get the first file found, parse the json file function getCfgFromJsonFile(dir, cb){ continue, since already get cfg (either pass or error) try to read the file fs.readFile( _filename, 'utf8', function readFile(e, data) { file not exist error, continue other error, means real error parse json string, and set returned cfg handle expect error continue function done(){ using default cfg if undefined log and return return rtnCfg;

function getcfg4busi: using business defaults for getcfg, including searchPaths = '../..', basedir and filename is derived from jsFilename (i.e. __filename), ext is '.json'

parameters: defCfgInfo: default cfg info jsFilename: the js file (with path), which seeks the its cfg file callback: errlist: array of errors, or [] if pass cfginfo: pure obj of cfg info, or null if failed function getcfg4busi(defCfgInfo, jsFilename, callback){ getcfg( defCfgInfo, _jsonCfgFile, _searchPaths, _basedir, function cb( rtnErrList, rtnCfgInfo ) { fail to get cfg, exit 4 pass to get cfg (either from json file, or using local json)


module.exports.getcfg = getcfg; module.exports.errlist2strlist = errlist2strlist module.exports.getcfg4busi = getcfg4busi;