Used to get config files in Tars framework
npm install @tars/config
Instantiate the object before using it:
var config = new TarsConfig (data)
data: The path to the config file or the configured @tars/config-parser
instance.
If the server runs by node-agent (or on Tars platform,then you don't need to pass data
。
Defines the format of the config file:
- C: C++ Config file formart
- JSON: JSON format
- TEXT: Normal text(Custom format)
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
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.
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);
});
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.
Get all config file names of server:
config.loadList().then(function(filelist) {
console.log("files:", filelist);
}, function(err) {
console.log("loadList error", err);
});
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.
Get default config file:
config.loadServerConfig().then(function(data) {
console.log("content:", data);
}, function(err) {
console.log("loadServerConfig error", err);
});
Emited when push config file to Tars platform.
The callback function while provide pushed filename.
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);
});
});