我尝试以我可以在与 mongo 一起保存的 nodejs 中使用会话的方式配置我的 connect-mongo。我使用以下代码
var mongoose = require('mongoose');
function connect(url, callback) {
mongoose.connect(url);
var connection = mongoose.connection;
connection.on('error', console.error.bind(console, 'connection error:'));
connection.once('open', function() {
console.log("Mongoose connected at: ", url);
callback(connection);
});
}
var express = require("express");
var body_parser = require('body-parser');
var cookie_parser = require('cookie-parser');
var hogan_express = require('hogan-express');
var session = require('express-session');
var mongo_store = require('connect-mongo')(session);
var express = require("express");
var app = express();
app.engine('html', hogan_express);
app.set('view engine', 'html');
app.set("views", "views");
app.use("/libs", express.static("bower_components"));
if (!config.development) {
app.use(express.static("min"));
}
app.use(express.static("public"));
connect("MONGODBURL",function(mongoose_connection){
app.use(body_parser.json());
app.use(cookie_parser());
app.use(session({
secret: "asd",
store: new mongo_store({
mongoose_connection: mongoose_connection
// db: mongoose_connection.db
})
}));
})
我已经尝试了所有方法,但执行了一些代码,我从来没有在我的 res refrence 中使用对象会话。
获取:TypeError:无法设置未定义的属性“asd”
代码:
app.get("/rest/test",function(req, res) {
req.session.asd="test";
res.send(req.session.asd)
});
有人可以给出解决方案的提示吗?