最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

网站源码admin16浏览0评论

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

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

我不确定要为我的问题加上什么标题。与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]);”正确

回答如下:

您应该在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)

  1. 暂无评论