Answer the question
In order to leave comments, you need to log in
Connect-mongo creates a new session on every page refresh?
Hello! Connect-mongo creates a new session every time the page is refreshed. In 10 minutes they can get ~ 50 pieces (and that's just me). Please help how to solve this problem?
The code
var express = require('express');
var MongoStore = require('connect-mongo')(express);
var pages = require('./routes/pages');
var db = require('mongoose').connect('mongodb://user:[email protected]:port/db');
var app = express();
var server = require('http').createServer(app).listen(process.env.PORT);
app.set('db', db);
app.set('views', 'views');
app.set('view engine', 'jade');
app.use(express.cookieParser());
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(express.session({
store: new MongoStore({
url: 'mongodb://user:[email protected]:port/db',
stringify: false
}),
cookie: {
maxAge: 24 * 360000
},
key: 'xxxxxx',
secret: 'ararar'
}));
app.use(app.router);
app.use(express.static('public'));
app.get('/', function (req, res) { res.end('hello world') });
Answer the question
In order to leave comments, you need to log in
As far as I remember it depends on app.use location sequences. Plus your code is non-standard for express.
I don’t know if it will help you, but personally, when describing app.use(express.session) I use a mongoose connection:
var db = require('mongoose').connect('mongodb://user:[email protected]:port/db');
app.use(express.session({
store: new MongoStore({
mongoose_connection: db.connections[0]
})
}));
I don’t create a new session every time I refresh the page.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question