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

编程入门 行业动态 更新时间:2024-10-10 18:24:21

用于Node.js的JSON中的<a href=https://www.elefans.com/category/jswz/34/1771377.html style=多个配置"/>

用于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);

更多推荐

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

本文发布于:2024-05-07 08:32:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1755408.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   Node   js   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!