nodejs。在MongoDB中我应该使用什么库来存储会话?

时间:2022-07-12 07:14:00

I've found three library of session storing in MongoDB: connect-mongodb, connect-mongo, connect-session-mongo

我在MongoDB中找到了三个会话库:connect-mongodb、connect-mongo、connect-session-mongo

Which is the best?

哪个是最好的?

EDIT: So if I'll use connect-mongodb I have to make two db connections. First for the session store:

编辑:如果我要使用connect-mongodb,我必须建立两个db连接。首先是会话商店:

var connect = require('connect')
  , Db = require('mongodb').Db
  , Server = require('mongodb').Server
  , server_config = new Server('localhost', 27017, {auto_reconnect: true, 
    native_parser: true})
  , db = new Db('test', server_config, {})
  , mongoStore = require('connect-mongodb');

connect.createServer(
  connect.bodyParser(),
  connect.cookieParser(),
  connect.session({
    cookie: {maxAge: 60000 * 20} // 20 minutes
  , secret: 'foo'
  , store: new mongoStore({db: db})
})
);

Second for my mongoose connection:

其次是我的猫鼬连接:

var mongoose = require('mongoose');
db = mongoose.connect('mongodb://localhost/test');
....

this is so?

这是如此吗?

3 个解决方案

#1


1  

https://github.com/masylum/connect-mongodb is listed on the 3rd party middleware (session stores) page of Connect, it has the most followers (111) and it's actively updated (last update ~ 8 hours ago), so I would probably pick that one if I were you.

https://github.com/masylum/connect-mongodb在Connect的第三方中间件(session stores)页面上列出,它拥有最多的关注者(111),并且正在积极更新(上一个更新~ 8小时前),所以如果我是你,我可能会选择那个。

Edit:

编辑:

About your second question, how to use connect-mongodb along with Mongoose, here's an example:

关于第二个问题,如何使用连接-mongodb和Mongoose,这里有一个例子:

var connect= require('connect'),
mongoose = require('mongoose'),
mongoStore = require('connect-mongodb'),
my_db = "mongodb://localhost/test",
db;

connect.createServer(
  ...
  connect.session({ store: mongoStore(my_db, secret: 'topsecret' }));
  ...
);

db = mongoose.connect(my_db);

Nodepad is a real app that uses Express, connect-mongodb and Mongoose, you can checkout the main app file here: https://github.com/alexyoung/nodepad/blob/master/app.js

Nodepad是一个真正的应用程序,它使用Express、connect-mongodb和Mongoose,你可以在这里签出主要的应用程序文件:https://github.com/alexyoung/nodepad/blob/master/app.js。

#2


2  

Connect-mongodb does the connection for you automatically. You pass in the string which is the database name and optionally a host (localhost is default). Here's how we're using it.

Connect-mongodb自动为您完成连接。您传入一个字符串,该字符串是数据库名,也可以是主机(默认是localhost)。这是我们使用它的方法。

app.use(express.session({
    secret: "x2kjh2323hjhjk32hjk23uhi23",
    store: new MongoStore({host: "200.11.11.11", db: 'sessions'}),
    cookie: {maxAge: 604800000}
}))

#3


0  

from API doc: unfortunately since Version 1.* I can't do that. This version is not compatible with 0.* versions. Now you must pass a mongodb connection, or server configuration. On updating, i recomment to delete your current sessions collection data

从API文档:不幸的是从版本1开始。我不能那样做。这个版本与0不兼容。*版本。现在必须传递mongodb连接或服务器配置。更新时,我重新注释以删除当前会话收集数据

#1


1  

https://github.com/masylum/connect-mongodb is listed on the 3rd party middleware (session stores) page of Connect, it has the most followers (111) and it's actively updated (last update ~ 8 hours ago), so I would probably pick that one if I were you.

https://github.com/masylum/connect-mongodb在Connect的第三方中间件(session stores)页面上列出,它拥有最多的关注者(111),并且正在积极更新(上一个更新~ 8小时前),所以如果我是你,我可能会选择那个。

Edit:

编辑:

About your second question, how to use connect-mongodb along with Mongoose, here's an example:

关于第二个问题,如何使用连接-mongodb和Mongoose,这里有一个例子:

var connect= require('connect'),
mongoose = require('mongoose'),
mongoStore = require('connect-mongodb'),
my_db = "mongodb://localhost/test",
db;

connect.createServer(
  ...
  connect.session({ store: mongoStore(my_db, secret: 'topsecret' }));
  ...
);

db = mongoose.connect(my_db);

Nodepad is a real app that uses Express, connect-mongodb and Mongoose, you can checkout the main app file here: https://github.com/alexyoung/nodepad/blob/master/app.js

Nodepad是一个真正的应用程序,它使用Express、connect-mongodb和Mongoose,你可以在这里签出主要的应用程序文件:https://github.com/alexyoung/nodepad/blob/master/app.js。

#2


2  

Connect-mongodb does the connection for you automatically. You pass in the string which is the database name and optionally a host (localhost is default). Here's how we're using it.

Connect-mongodb自动为您完成连接。您传入一个字符串,该字符串是数据库名,也可以是主机(默认是localhost)。这是我们使用它的方法。

app.use(express.session({
    secret: "x2kjh2323hjhjk32hjk23uhi23",
    store: new MongoStore({host: "200.11.11.11", db: 'sessions'}),
    cookie: {maxAge: 604800000}
}))

#3


0  

from API doc: unfortunately since Version 1.* I can't do that. This version is not compatible with 0.* versions. Now you must pass a mongodb connection, or server configuration. On updating, i recomment to delete your current sessions collection data

从API文档:不幸的是从版本1开始。我不能那样做。这个版本与0不兼容。*版本。现在必须传递mongodb连接或服务器配置。更新时,我重新注释以删除当前会话收集数据