我正在使用express session
和MongoStore
来存储会话ID
这是我的app.js
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var passport = require('passport');
var flash = require('connect-flash');
var path = require('path');
var app = express();
var user = require('./models/userModel');
//------Connect to database
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/porto', { useMongoClient: true });
//------Config Passport
require('./config/passport.js')(passport);
//------Adding middlewares
app.use(bodyParser.urlencoded({extended:true}));
app.use(cookieParser());
app.use(session({
secret : 'secret',
saveUninitialized : true,
resave : true,
store : new MongoStore({mongooseConnection : mongoose.connection})
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
我发布ToDo的路线处理程序
app.post('/addTodo',function(req,res){
console.log(req.body);
console.log(req.user._id); // Here the user id is changed
console.log(req.sessionID); // Here the sessionID never changed, but assigned to the newly logged in user
var todo = req.body.todo;
userModel.update(
{ _id: req.user._id },
{ $push: { todos: todo } },
function(err,done){
if (err)
return err;
return done;
}
);
res.redirect('/profile');
});