skip to Main Content

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

2

Answers


  1. 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);
    
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search