Recently when using TypeScript. Write a backend interface, you need to read the yaml configuration file, use the yaml the nodejs library, the NPM’s website address is: www.npmjs.com/package/yam… Github source code address: github.com/eemeli/yaml For example, the following config.yaml configuration file:

rxmqtt:
  host:     127.0. 01.
  port:     11883
  user:     testuser
  pwd: "123456"
  id: "this_is_test_2000804_nodejs_water"
  clean:    true
dbsql:
  host: 127.0. 01.
  port: 3306
  user: root
  pwd: "123456"
  database: testdb
redis: 
  host:     127.0. 01.
  port:     7001
  pwd:  123456
  index:     0
http: 8088
rpcUrl: 127.0. 01.: 18885
enableMqtt: true
enableDB: true
enableRedis: true
enableWS: true
enableRPC: false
offlineTimeout: 90000
cacheInterval: 10000
Copy the code

Use typescript to write the config.ts file for the above config.yaml file as follows:

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 port */
  http: number;
  /** * rpcUrl server address */
  rpcUrl: string;
  /** * whether to enable MQTT */
  enableMqtt: boolean;
  /** * Whether to enable mariadb */
  enableDB: boolean;
  /** * Whether to enable redis */
  enableRedis: boolean;
  /** * Whether to enable websocket */
  enableWS: boolean;
  /** * Whether to enable RPC */
  enableRPC: boolean;
  /** * Offline timeout in milliseconds */
  offlineTimeout: number;
  /** * Cache storage interval, milliseconds */
  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)
    }
  }
}
Copy the code

The corresponding config.js file is as follows:

"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;Copy the code

Related references

  • www.npmjs.com/package/yam…
  • github.com/eemeli/yaml
  • Parse yamL files using Node