如何使用ssh-tunnel mongo连接和存储进行快速会话?

时间:2018-06-11 17:54:38

标签: node.js mongodb express session

带快递服务器的索引文件:

    module.exports = require('../mongo');

    // initialize express-session to allow us track the logged-in user across sessions.
    const MongoStore = require('connect-mongo')(session);
    app.use(session({
        key: 'user_sid',
        // mongoose.connection
        store: new MongoStore({ mongooseConnection: mongoose.connection }),
        secret: 'somerandonstuffs',
        resave: false,
        saveUninitialized: false,
        cookie: {
            expires: 600000
        }
    }));

Mongo与mongoose的连接我使用tunnel-ssh进行远程连接:

const config = {
    ...configSettings
};

const server = tunnel(config, function (error, server) {

    mongoose.connect(process.env.SSH_DB);

    const db = mongoose.connection;

    db.once('open', function() {
        console.log("DB connection successful");
    });
});

我通过ssh-tunnel连接到服务器,连接成功。但是当我想用mongoStore添加一个快速会话时,我有一个错误:

store: new MongoStore({ mongooseConnection: mongoose.connection })
ReferenceError: mongoose is not defined

我知道mongoose实例未在索引文件中定义,但我不知道如何正确导出它:(

0 个答案:

没有答案