2015-06-21 76 views
1

我試圖讓會話在Express v4中存儲通行證會話,並且在服務器重新啓動後它不會保留。我已經在使用connect-mongo,所以我不清楚爲什麼這個目前沒有持續。在node.js重新啓動後繼續保持會話

db.js

module.exports = function(app, config) { 
    var mongoose = require('mongoose'), 
    Schema = mongoose.Schema, 
    session = require('express-session'), 
    MongoStore = require('connect-mongo')(session), 
    fs = require('fs'); 

    mongoose.connect(config.db); 

    mongoose.connection.on('error', console.error.bind(console, 'connection error:')); 
    mongoose.connection.once('open', function callback() { 
     console.info('Database Connected!'); 
    }); 

    var sessionStore = new MongoStore({mongooseConnection: mongoose.connection}); 
    module.exports = sessionStore; 
}; 

express.js

module.exports = function (app, config, passport, dbConnect) { 
    /* #Flash messaging and sessions ------------------------------------------------- */ 
    app.use(cookieParser('secretStrings')); 
    app.use(session({ 
     secret: 'secretStrings', 
     resave: true, 
     saveUninitialized: true, 
     cookie: { 
      path: '/', 
      httpOnly: true, 
      secure: false, 
      maxAge: null 
     }, 
     rolling: true, 
     store: dbConnect 
    })); 
    app.use(flash()); 

    /* #Set up passport ------------------------------------------------- */ 
    // use passport session 
    app.use(passport.initialize()); 
    app.use(passport.session()); 
}; 

Server.js

/* #Mongo --------------------------------------------------------------------- */ 
var db = require('./app/db')(app, config); 

/* #Express Settings --------------------------------------------------------------------- */ 
require('./config/express')(app, config, passport, db); 

我相信只有對此的合乎邏輯的解釋是,我將文件以錯誤的順序包含在內,因此會話無法保存?

+1

在你的'db.js'中不應該'module.exports = sessionStore;'而不是'return sessionStore;'? – mscdex

+0

哈哈,當然它應該。當我使用express.js而不是db.js時,我粘貼了這個副本!嗯添加它作爲答案,我會接受它。雖然我想知道,我應該刪除這個問題作爲一個非常具體的問題。 –

回答

1

sessionStore需要return編在db.js而不是module.exports = sessionStore;

相關問題