使用Mongodb副本集,前端express如何保存session
发布于 10 年前 作者 hanzel21cn 4188 次浏览 最后一次编辑是 8 年前

请教大家一个问题,我在后台有2个mongodb,运用了副本集 那么我在express中 store: new MongoStore({ url: require(’./config’).db })

这个url应该怎么配置呢?

2 回复

使用connect-mongostore https://github.com/diversario/connect-mongostore

app.use(express.session({
    secret: 'my secret',
    store: new MongoStore(replicaSet)
  }));
var replicaSet = {
  "collection" : "express_sessions",
  "stringify": false,
  "db": {
    "name" : "sessions",
    "servers" : [
      {
        "host" : "localhost",
        "port" : 27017,
        "options" : {
          "autoReconnect" : false,
          "poolSize" : 200,
          "socketOptions" : {
            "timeout" : 0,
            "noDelay" : true,
            "keepAlive" : 1,
            "encoding" : "utf8"
          }
        }
      },
      {
        "host" : "localhost",
        "port" : 27018,
        "options" : {
          "autoReconnect" : false,
          "poolSize" : 200,
          "socketOptions" : {
            "timeout" : 0,
            "noDelay" : true,
            "keepAlive" : 1,
            "encoding" : "utf8"
          }
        }
      },
      {
        "host" : "localhost",
        "port" : 27019,
        "options" : {
          "autoReconnect" : false,
          "poolSize" : 200,
          "socketOptions" : {
            "timeout" : 0,
            "noDelay" : true,
            "keepAlive" : 1,
            "encoding" : "utf8"
          }
        }
      }
    ]
  }
}

太感谢您了,一会实验一下

回到顶部