@ksfcore/config
v2.0.5
Published
KSF 框架中用于获取服务配置文件
Downloads
2
Readme
Ksf-Config
Used to get config files in Ksf framework
Installation
npm install @ksfcore/config
Instantiation
Instantiate the object before using it:var config = new KsfConfig (data)
data: The path to the config file or the configured @ksfcore/config-parser
instance.
If the server runs by node-agent (or on Ksf platform,then you don't need to pass data
。
Enum
FORMAT
Defines the format of the config file:
- C: C++ Config file formart
- JSON: JSON format
- TEXT: Normal text(Custom format)
LOCATION
Defines the position where the config files are stored:
- APP: Config files are stored in the application
- SERVER: Config files are stored in the servers
Methods
loadConfig([files ,]options)
Get config file content。
files(String|Array)
It can be a single file name or an array, and if it is left empty, all the file contents will be obtained by default.
options(Object)
Optional, accept the following parameters:
- format: File format, default to e FORMAT.C
- location: Stored position, default to be LOCATION.SERVER
Return array of following objects after call success:
- filename: Filename
- content: Content after file parsing
If only get one single file, it will return the parsed content of the file.
Example
Get the content of a.conf
:
config.loadConfig("a.conf").then(function(data) {
console.log("content:", data);
}, function (err) {
console.error("loadConfig err", err);
});
Get the content of a.conf
and parse by json:
config.loadConfig("a.conf", {format : config.FORMAT.JSON}).then(function(data) {
console.log("content:", data);
}, function (err) {
console.error("loadConfig err", err);
});
Get content of a.conf
which is stored in application:
config.loadConfig("a.conf", {location : config.LOCATION.APP}).then(function(data) {
console.log("content:", data);
}, function (err) {
console.error("loadConfig err", err);
});
Get content of a.conf
and b.conf
:
config.loadConfig(["a.conf", "b.conf"]).then(function(data) {
data.forEach(function(item) {
console.log("filename:", item.filename);
console.log("content:", item.content);
});
}, function (err) {
console.error("loadConfig err", err);
});
Get all config files of server:
config.loadConfig().then(function(data) {
data.forEach(function(item) {
console.log("filename:", item.filename);
console.log("content:", item.content);
});
}, function (err) {
console.error("loadConfig err", err);
});
loadList(options)
Get list of config files(name of all config files)。
options(Object)
Optional, accept the following parameters:
- location: Stored position, default to be LOCATION.SERVER
Return array of file names after call success.
Example
Get all config file names of server:
config.loadList().then(function(filelist) {
console.log("files:", filelist);
}, function(err) {
console.log("loadList error", err);
});
loadServerConfig(options)
Get default config file(name is like App.Server.conf
)。
options(Object)
Optional, accept the following parameters:
- format: File format, default to be FORMAT.C
Return parsed Content of config file after call success.
Example
Get default config file:
config.loadServerConfig().then(function(data) {
console.log("content:", data);
}, function(err) {
console.log("loadServerConfig error", err);
});
Events
configPushed
Emited when push config file to Ksf platform.
The callback function while provide pushed filename.
Example
Listen to push event, and get pushed file content :
config.on("configPushed", function(filename) {
console.log("config pushed", filename);
config.loadConfig(filename).then(function(data) {
console.log("content:", data);
}, function(err) {
console.error("loadConfig err", err);
});
});