用于Node.js的JSON中的多个配置

问题描述 投票:0回答:2

我不确定要为我的问题加上什么标题。与node.js一起经历了一次冒险,一个乐于助人的人向我介绍了ioredis。目前我有:

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",
    },

]);

但是对我来说,这似乎在json配置文件中会更好,例如...

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",
    },
}

我很新,我只是不确定如何在没有语法错误的情况下实现它。

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

我不确定如何实现“ var cluster = new Redis.Cluster([DBConfig.redis]);”正确

node.js json
2个回答
0
投票

您应该在key]下的array中声明这些设置

{
  "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"
    }
  ]
}

然后使用该键访问所需的配置文件中的该值。

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

0
投票

首先,您需要具有正确的配置文件。您的文件似乎包含一些配置信息和节点信息。我建议:

© www.soinside.com 2019 - 2024. All rights reserved.