前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >nodejs库yaml读取yml或yaml配置文件

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

作者头像
ccf19881030
发布2020-08-05 15:37:31
4.5K0
发布2020-08-05 15:37:31
举报
文章被收录于专栏:ccf19881030的博客ccf19881030的博客

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

相关参考资料

  • https://www.npmjs.com/package/yaml
  • https://github.com/eemeli/yaml
  • 使用node解析yaml文件
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-08-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 相关参考资料
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档