A configparser based on nodejs
npm install huafua-configparserapp.conf的形式引入我们的配置,但是里头的都是字符窜,想要单独处理下又觉得浪费时间,这时候本模块就起作用了。
conf
[section]
optionName=optionValue
optionName1=optionValue1
`
1. 几个Api
该模块向外提供了ConfigOption、ConfigSection和ConfigParser类:
支持config.section.option的形式获取配置项的值
ConfigParser提供的Api:
- addSection(section:ConfigSection):添加节点
- addSections(sections:Array:批量添加节点
- addOption(sectionName:string,option:ConfigOption):添加配置项
- addOptions(sectionName:string,options:Array:批量添加配置项
- readAsync(configpath:string):异步形式读取配置文件,配置文件路径必须为绝对路径
- read(configpath:string):同步形式读取配置文件,配置文件路径必须为绝对路径
- get(sectionName?:string,optionName?:string):获取指定节点或配置项的值,当没有给定参数则直接返回该配置的json数据
- getInt(sectionName:string,optionName:string):获取指定配置项的int值
- getString(sectionName:string,optionName:string):获取指定配置项的string值
- getJson(sectionName?:string):获取指定节点的json对象,若没有指定参数则获取当前配置的json对象
- set(sectionName:string,optionName:string,optionValue:string|number):设置指定节点的配置项
- save(distPath:string):保存当前配置到指定文件,文件路径须为绝对路径
- saveAsJson(distPath?:string):保存当前配置json数据到指定文件,文件路径须为绝对路径
- clear():清空配置
- remove(sectionName:string,optionName?:string):删除指定节点或配置项
ConfigSection提供的Api:
- ConfigSection(sectionName:string):构造方法,传递节点名称实例化对象
- init(options:Array:通过配置项数组初始化配置节点
- addOption(option:ConfigOption):添加配置项
- addOptions(options:Array:批量添加配置项
- get(optionName:string):获取当前节点指定配置项的值
- getInt(optionName:string):获取当前节点指定的配置项int值
- getString(optionName:string):获取当前节点指定配置项的string值
- getJson():获取配置节点的json数据
- set(optionName:string,optionValue:string|number):该节点设置一个配置项
- remove(optionName:string):删除指定配置项
2. 使用
1. 导入
`js
var ConfigParser=require("huafua-configparser");
`
2. 实例化
`js
var config=new ConfigParser();
`
3. 指定配置文件
`js
config.read(path.resolve(__dirname,"app.conf"));
``