开发者

connect-mongodb access sessions

开发者 https://www.devze.com 2023-04-13 04:46 出处:网络
In my node.js app I am using expressJS and connect-mongodb for the session store. I have set connect-mongodb to point to me local my local MongoDB (e.g. mongoStore(mongodb://localhost/myAppDB\')

In my node.js app I am using expressJS and connect-mongodb for the session store. I have set connect-mongodb to point to me local my local MongoDB (e.g. mongoStore(mongodb://localhost/myAppDB')

All my u开发者_StackOverflow社区ser authentication works as expected, but I am not seeing a sessions collection in my mongodb. For example when I run the following in the terminal:

$ mongo myAppDB
 > show collections

I see all my other collection, but no sessions.

Should I be able to see them this way?

I just want to be able to access my session from the connect.sid, but since mongoStore has no 'get' method, so I am trying to pull the session directly from the database


According to the connect-mongodb documentation (https://github.com/masylum/connect-mongodb) version 1.x is not compatible with 0.x versions. In earlier versions you could pass an URL. Now you must pass a mongodb connection, or server configuration.

The following shows how I it is in my app (essentially copied the example in the connect-mongodb docs):

var express = require('express');
var Db = require('mongodb').Db;
var Server = require('mongodb').Server;
var server_config = new Server('localhost', 27017, {auto_reconnect: true, native_parser: true});
var mongoDatabase;

mongoDatabase = new Db(app.set('db-name'), server_config, {});

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.cookieParser()); 
  app.use(express.session({
    store: new mongoStore({ db: mongoDatabase }),
    secret: 'topsecret'
  }));
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号