如何使用 Express.js/Nodemon 获得持久会话? [英] How do I get a persistent session working with Express.js/Nodemon?

查看:74
本文介绍了如何使用 Express.js/Nodemon 获得持久会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 TypeScript 服务器基于 这个示例项目.每当 nodemon 重新启动我的服务器时,无论是自动还是手动,我都会丢失会话并且必须重新登录.我讨厌这样做,因为第 1000 次这样做是在浪费大量时间.我尝试配置我的 Express.js 会话设置,但似乎没有任何效果.这是我的配置:

My TypeScript server is based on this example project. Whenever nodemon restarts my server, whether automatic or manually, I lose my session and have to log in again. I hate this because it's a massive waste of time when done for the 1000th time. I've tried to configure my Express.js session settings, but nothing seems to be working. Here's my configuration:

/**
* Configure application
*
* @class Server
* @method config
*/
public config() {
    //add static paths
    this.app.use(express.static(path.join(__dirname, 'public')));

    //configure pug
    this.app.set('views', path.join(__dirname, 'views'));
    this.app.set('view engine', 'ejs');

    //use logger middlware
    this.app.use(logger('dev'));

    //use json form parser middlware
    this.app.use(bodyParser.json());

    //use query string parser middlware
    this.app.use(bodyParser.urlencoded({
        extended: true
    }));

    this.app.use(cookieParser("my secret"));
    this.app.use(session({
        name: 'MyStudio',
        secret: "my secret",
        resave: false,
        saveUninitialized: false,
        cookie: {
            maxAge: 86400000,
            httpOnly: true,
            secure: false
        }
    }));
    this.app.use(passport.initialize());
    this.app.use(passport.session());
    //this.app.set('trust proxy', 1) // trust first proxy

    this.app.set('json spaces', 4);

    passport.serializeUser((user, done) => {
        return done(null, user);
    });

    passport.deserializeUser((user, done) => {
        return done(null, user);
    });

    passport.use('local-login', new Local.Strategy(
        (username, password, done) => {
            // My custom login logic
        }
    ));

    this.app.use(methodOverride());
}

如何阻止 Express.js 在每次重启时丢失会话?

How do I stop Express.js from losing my session on every restart?

推荐答案

express-session 支持不同的 store(数据库)来存储 session 数据.

express-session has support for different stores (databases) to store the session data in.

使用的默认值是 MemoryStore,它将会话数据保持在内存中只要 Express 进程运行.当 Express 进程停止时(例如,当它重新启动时),所有会话数据都消失了.

The default that is used is MemoryStore which keeps session data in memory for as long as the Express process runs. When the Express process stops (for instance, when it restarts), all session data is gone.

如果您希望会话数据在重启后继续存在,您需要使用持久会话存储.它们不是将会话数据存储在内存中,而是将会话数据存储在(例如)数据库中.有很多可用的持久会话存储,可以在此处找到其中的列表:https://github.com/expressjs/session#compatible-session-stores

If you want session data to survive restarts, you need to use a persistent session store. Instead of storing the session data in memory, these store the session data in (for instance) a database. There are plenty of persistent session stores available, a list of which can be found here: https://github.com/expressjs/session#compatible-session-stores

这篇关于如何使用 Express.js/Nodemon 获得持久会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆