我将配置从另一个库迁移到节点配置。
我有两个问题:
config.get('a:b');来获得一些值,但是node-config使用一个点作为分隔符:config.get('a.b');。是否有一种方法可以将其配置为使用:来节省时间和重构代码?
config.set('key', 'val');发布于 2020-04-24 15:20:08
通过: 1.将node-config包装到一个新的js文件中。2.代理get、has和set方法方法。
就像这样:
const config = require('config');
const inMemDict = {};
const toNewKey = key => {
return key && key.split(':').join('.');
};
const { get: origGet, has: origHas } = config;
config.get = function (key, ...args) {
key = toNewKey(key);
if(typeof inMemDict[key] !== 'undefined') {
return inMemDict[key];
}
return origGet.apply(config, [key, ...args]);
};
config.has = function (key, ...args) {
key = toNewKey(key);
if(typeof inMemDict[key] !== 'undefined') {
return inMemDict[key];
}
return origHas.apply(config, [key, ...args]);
};
config.set = function (key, val) {
if(!key) return;
inMemDict[toNewKey(key)] = val;
};
module.exports = config;https://stackoverflow.com/questions/61198118
复制相似问题