最近在使用TypeScript编写后台接口时,需要读取yaml配置文件,使用到了yaml这个nodejs库,其npm官网地址为:https://www.npmjs.com/package/yaml,github源代码地址为:github.com/eemeli/yaml 比如有如下的config.yaml配置文件:
rxmqtt:
host: 127.0.0.1
port: 11883
user: testuser
pwd: "123456"
id: "this_is_test_2000804_nodejs_water"
clean: true
dbsql:
host: 127.0.0.1
port: 3306
user: root
pwd: "123456"
database: testdb
redis:
host: 127.0.0.1
port: 7001
pwd: 123456
index: 0
http: 8088
rpcUrl: 127.0.0.1:18885
enableMqtt: true
enableDB: true
enableRedis: true
enableWS: true
enableRPC: false
offlineTimeout: 90000
cacheInterval: 10000
使用typescript针对上述config.yaml文件编写对应的config.ts文件如下:
import YAML = require('yaml')
import fs = require('fs')
declare interface MqttConnOpt{
host: string;
port: number;
user: string;
pwd: string;
clean: boolean;
id: string;
}
declare interface DBConnOpt{
host: string;
port: number;
user: string;
pwd: string;
database: string;
}
declare interface RedisConnOpt{
host: string;
port: number;
pwd: string;
db: number;
}
export {
MqttConnOpt,
DBConnOpt,
RedisConnOpt,
Config,
}
class Config {
rxmqtt: MqttConnOpt;
dbsql: DBConnOpt;
redis: RedisConnOpt;
/**
* http 端口
*/
http: number;
/**
* rpcUrl 服务器地址
*/
rpcUrl: string;
/**
* 是否启用mqtt
*/
enableMqtt: boolean;
/**
* 是否启用mariadb
*/
enableDB: boolean;
/**
* 是否启用redis
*/
enableRedis: boolean;
/**
* 是否启用websocket
*/
enableWS: boolean;
/**
* 是否启用RPC
*/
enableRPC: boolean;
/**
* 离线超时时间, 毫秒
*/
offlineTimeout: number;
/**
* 缓存存储间隔, 毫秒
*/
cacheInterval: number;
constructor(){
try{
let buffer = fs.readFileSync('config.yaml', 'utf8');
let config = YAML.parse(buffer);
this.rxmqtt = config['rxmqtt'];
this.dbsql = config['dbsql'];
this.redis = config['redis'];
this.http = config['http'];
this.rpcUrl = config['rpcUrl'];
this.enableMqtt = config['enableMqtt'];
this.enableDB = config['enableDB'];
this.enableRedis = config['enableRedis'];
this.enableWS = config['enableWS'];
this.enableRPC = config['enableRPC'];
this.offlineTimeout = config['offlineTimeout'];
this.cacheInterval = config['cacheInterval'];
}catch(err){
console.log(err)
}
}
/**
* save
*/
public save() {
try{
fs.writeFileSync('config.yaml', YAML.stringify(this))
}catch(err){
console.log(err)
}
}
}
对应的config.js文件如下所示:
"use strict";
exports.__esModule = true;
exports.Config = void 0;
var YAML = require("yaml");
var fs = require("fs");
var Config = /** @class */ (function () {
function Config() {
try {
var buffer = fs.readFileSync('config.yaml', 'utf8');
var config = YAML.parse(buffer);
this.rxmqtt = config['rxmqtt'];
this.dbsql = config['dbsql'];
this.redis = config['redis'];
this.http = config['http'];
this.rpcUrl = config['rpcUrl'];
this.enableMqtt = config['enableMqtt'];
this.enableDB = config['enableDB'];
this.enableRedis = config['enableRedis'];
this.enableWS = config['enableWS'];
this.enableRPC = config['enableRPC'];
this.offlineTimeout = config['offlineTimeout'];
this.cacheInterval = config['cacheInterval'];
}
catch (err) {
console.log(err);
}
}
/**
* save
*/
Config.prototype.save = function () {
try {
fs.writeFileSync('config.yaml', YAML.stringify(this));
}
catch (err) {
console.log(err);
}
};
return Config;
}());
exports.Config = Config;