I have strange problem, don't know what happened... I try use connect-mongo with express-session, but it's not work. What i do... I create server.js file who look like this
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
function Start() {
var app = express();
// Include views
app.engine('ejs', require('ejs-locals'));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// Include vendor lib's
app.use(express.static(path.join(__dirname, '../public')));
app.use("/public", express.static(path.join(__dirname, '../public')));
app.use(express.static(path.join(__dirname, 'public')));
app.use(favicon('public/favicon.ico'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
//It's a connection to DB. I wait until the connection is established and create global DB object.
require('./db')(function(err) {
if (err) {
log.crit('DB connection error!');
throw err;
} else {
// After connection established i use session.
app.use(session({
secret: config.get('session:secret'),
key: config.get('session:key'),
cookie: {
path: "/",
httpOnly : true,
maxAge : null
},
store: new MongoStore({mongoose_connection: DB.MONGO.connection}),
resave: true,
saveUninitialized: true
}));
// And after i use routes.
var routes = require('./routes')(express);
app.use(routes);
app.listen(port, ipaddress, function() {
log.notice('Node server started on ' + [ipaddress, port].join(':'));
cron.dbSync(function(err, cj) {
...
});
});
}
});
};
For DB connection i use mongoose. And DB.MONGO included mongoose object.
Ok. I run server.js, and all worked perfectly, routing, DB access, rendering... But when i try use req.session an error
/node_modules/express-session/index.js:259
req.session.resetMaxAge();
^
TypeError: Object #<Object> has no method 'resetMaxAge'
at ServerResponse.end (/node_modules/express-session/index.js:259:19)
at ServerResponse.res.redirect (/node_modules/express/lib/response.js:724:8)
at Promise.<anonymous> (/routes/adminpanel/login.js:28:21)
at Promise.<anonymous> (/node_modules/mpromise/lib/promise.js:177:8)
at Promise.emit (events.js:95:17)
at Promise.emit (/node_modules/mongoose/node_modules/mpromise/lib/promise.js:84:38)
at Promise.fulfill (/node_modules/mongoose/node_modules/mpromise/lib/promise.js:97:20)
at /Users/arthurbortnik/GitHub/GMX/node_modules/mongoose/lib/query.js:1393:13
at model.Document.init (/node_modules/mongoose/lib/document.js:250:11)
at completeOne (/node_modules/mongoose/lib/query.js:1391:10)
I spent a lot of time to fix the problem and found that it occurs after I try to run res.redirect ('/ adminpanel / stocks / all');
if (admin.isConfirmed && admin.checkPassword(req.body.password)) {
req.session = {
adminpanel: {
admin : admin,
auth : true
}
};
console.log(req.session);
res.redirect('/adminpanel/stocks/all');
}
Who know what happened?
Regards, Arthur Borg.
You are overwriting your session object when you do this:
req.session = {
adminpanel: {
admin : admin,
auth : true
}
};
I'm not sure what you're trying to achieve here but this is where you're losing the actual session object used by Express. There is a similar issue posted on github with a response from Doug Wilson.
https://github.com/expressjs/session/issues/82
This is a bug in your own app--you or some module you are using is overwriting req.session (i.e. doing a req.session = {} or something). Overwriting req.session is not supported in this module, as req.session needs to be an object with certain properties.