How to configure connect-mongo (or use sessions in mongoose)

I am using ExpressJS and Mongoose.

var MongoStore = require('connect-mongo')(express);
var sessionStore = new MongoStore({db: 'myappsession'});

app.use(express.session({ secret: "myappsecret", store:sessionStore }));

The result is "MongoError: Error: unauthorized db". I suppose I will need to give him my credentials. I also have

var mongoose = require('mongoose');
var db = mongoose.createConnection('<omitted username, password and address>', 'myappsession');

I assume connect-mongo needs this information to log into my database to create a session repository?

Question

How to transfer login information? Or am I doing it wrong?

+5
source share
2 answers

In my case, since I am already actively using Mongoose, I decided to use the solution for Mongoose.

1. Setting the mongoose session

https://github.com/donpark/session-mongoose

2. Use this tutorial as a guide.

http://mikevalstar.com/Blog/107/Coding_with_Nodejs_Part_31_Mongoose_Sessions

3. , .

url: "mongodb://localhost/mv"

- :

url: "username:password@url/testdatabase"

"testdatabase" .

, - .:)

+3

URI MongoDB. : http://www.mongodb.org/display/DOCS/Connections

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

, , :

mongoose.createConnection('mongodb://[username:password@]host1[:port1]', 'myappsession');
+1

All Articles