简体   繁体   中英

Multiple Config in JSON for Node.js

I am not sure what to title my question. Its been a adventure with node.js and a helpful person pointed me to ioredis. Currently I have:

var Redis = require("ioredis");
const DBConfig = require(__dirname+'/../Config.json');
var cluster = new Redis.Cluster([
    {
        port: 6001,
        host: "10.0.0.6",
    },
    {
        port: 6002,
        host: "10.0.0.5",
    },
    {
        port: 6003,
        host: "10.0.0.4",
    },
    {
        port: 6004,
    host: "10.0.0.3",
},
{
    port: 6005,
    host: "10.0.0.2",
    },
    {
        port: 6006,
        host: "10.0.0.1",
    },

]);

But to me this seems it would be better in a json config file like...

Config.json:

{
    "configA" : "abc",
    "someotherconfigB" : "Stuff",
    "foo" : "bar"
}
{
        "port": 6001,
        "host": "10.0.0.6",
    },
    {
        "port": 6002,
        "host": "10.0.0.5",
    },
    {
        "port": 6003,
        "host": "10.0.0.4",
    },
    {
        "port": 6004,
        "host": "10.0.0.3",
    },
    {
        "port": 6005,
        "host": "10.0.0.2",
    },
    {
        "port": 6006,
        "host": "10.0.0.1",
    },
}

I am so new and this I just not sure how to implement this without syntax errors.

var Redis = require("ioredis");
const DBConfig = require(__dirname+'/../Config.json');
var cluster = new Redis.Cluster([DBConfig.redis]);

I am not sure how to implement "var cluster = new Redis.Cluster([DBConfig.redis]);" properly

You should declare those settings in as an array under a key

{
  "configA" : "abc",
  "someotherconfigB" : "Stuff",
  "foo" : "bar",
  "redisCluster": [
    {
      "port": 6001,
      "host": "10.0.0.6"
    },
    {
      "port": 6002,
      "host": "10.0.0.5"
    },
    {
      "port": 6003,
      "host": "10.0.0.4"
    }
  ]
}

Then use that key to access that value inside the required config file.

const DBConfig = require('../Config.json');
const cluster = new Redis.Cluster(DBConfig.redisCluster);

First, you need to have a proper config file. Your file seems to contain some config information and node information. I would suggest:

Config.json file:

{
  "configs": {
    "configA": "abc",
    "someotherconfigB": "Stuff",
    "foo": "bar"
  },
  "nodes": [
    {
      "port": 6001,
      "host": "10.0.0.6"
    },
    {
      "port": 6002,
      "host": "10.0.0.5"
    },
    {
      "port": 6003,
      "host": "10.0.0.4"
    },
    {
      "port": 6004,
      "host": "10.0.0.3"
    },
    {
      "port": 6005,
      "host": "10.0.0.2"
    },
    {
      "port": 6006,
      "host": "10.0.0.1"
    }
  ]
}

Then your file should look like:

const Redis = require('ioredis');
const DBConfig = require(__dirname + '/Config.json');

const cluster = new Redis.Cluster(DBConfig.nodes);

Object.entries(DBConfig.configs).map(([key, value]) => {
  cluster.set(key, value);
});

DBConfig.nodes it's already an array. No need to put brackets around it

Object.entries(DBConfig.configs) will give you an array of [key, value] pairs of your DBConfig.configs 's properties

Resources:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM