【问题标题】:Controlling Session Start with LocomotiveJS用 LocomotiveJS 控制会话开始
【发布时间】:2013-09-13 03:29:20
【问题描述】:

我知道可以使用 express 和连接中间件来控制会话启动。喜欢这里的建议:Controlling Session Start with express and connect middleware

是否可以使用 LocomotiveJS 控制会话开始?

我不想为每个用户启动会话 - 我只想在用户成功登录时启动它。

我已经在 MongoDb 中存储了会话(未来我将使用 Redis)并且为每个进入页面的用户创建不需要的会话似乎是一种资源浪费。

【问题讨论】:

    标签: node.js locomotivejs


    【解决方案1】:

    我刚刚找到了解决方案: 可能 LcomotiveJS 本身并不直接支持,但是使用 Express 可以很容易地实现(而 Locomotive 就在它之上)。

    而不是将会话声明为:

    this.use(express.cookieParser());
    this.use(express.session({
        secret: '123qwemax',
        cookie: {maxAge: 60 * 60 * 1000},
        store: new MongoStore({
          url: 'mongodb://localhost:27017/tests'       
        }),
    }));
    

    将您的会话声明为:

    this.get(/^\/login\/.*/, express.cookieParser());
    this.get(/^\/login\/.*/, express.session({
        secret: '123qwemax',
        cookie: {maxAge: 60 * 60 * 1000},
        store: new MongoStore({
          url: 'mongodb://localhost:27017/tests'       
        }),
    }));
    

    这样会话将在我们进入 /login/.* 页面的任何时候开始。

    我不知道为什么 this.all 不起作用...

    【讨论】:

    • 当然,示例不包括仅为登录用户启动会话的逻辑。它只是表明会话的处理函数可以添加到定义的 url - 不是每个(就像 this.use 一样)。
    猜你喜欢
    • 2023-04-03
    • 2022-10-12
    • 1970-01-01
    • 2010-11-09
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    相关资源
    最近更新 更多