nodejs库yaml读取yml或yaml配置文件

最近在使用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;

相关参考资料

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值