我有奇怪的问题,不知道发生了什么...... 我尝试使用connect-mongo和express-session,但它不起作用。 我所做的... 我创建看起来像这样的server.js文件
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) {
...
});
});
}
});
};
对于数据库连接,我使用mongoose。 DB.MONGO包含了猫鼬对象。
确定。我运行server.js,所有工作都很完美,路由,数据库访问,渲染...但是当我尝试使用req.session时出错
/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)
我花了很多时间来解决问题并发现它在我尝试运行res.redirect后出现(&#39; / adminpanel / stocks / all&#39;);
if (admin.isConfirmed && admin.checkPassword(req.body.password)) {
req.session = {
adminpanel: {
admin : admin,
auth : true
}
};
console.log(req.session);
res.redirect('/adminpanel/stocks/all');
}
谁知道发生了什么?
此致,Arthur Borg。
答案 0 :(得分:1)
执行此操作时,您将覆盖会话对象:
req.session = {
adminpanel: {
admin : admin,
auth : true
}
};
我不确定你要在这里实现什么,但这是你失去Express
使用的实际会话对象的地方。在github上发布了一个类似的问题,Doug Wilson的回复。
https://github.com/expressjs/session/issues/82
这是您自己的应用中的错误 - 您或您正在使用的某个模块 覆盖req.session(即做一个req.session = {}或者某事)。 此模块不支持覆盖req.session,因为 req.session需要是具有某些属性的对象。