在node.js重启后保持Session

时间:2015-06-21 11:19:51

标签: javascript node.js mongodb session

我正在尝试在快递v4中存储会话以存储护照会话,并且在服务器重新启动后它不会持续存在。我已经在使用connect-mongo,所以我不清楚为什么现在还没有这样做。

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 个答案:

答案 0 :(得分:1)

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