我知道可以使用express控制会话启动并连接中间件。像这里建议的那样:Controlling Session Start with express and connect middleware
是否可以使用LocomotiveJS控制会话启动?
我不想为每个用户启动会话 - 我只想在用户成功登录时启动它。
我已经在MongoDb中存储会话了(将来myabe我将使用Redis)并且为每个进入该页面的用户创建不需要的会话似乎是浪费资源。
答案 0 :(得分:0)
我刚刚找到解决方案: 也许LcomotiveJS本身并不直接支持它,但可以使用Express轻松实现(而机车就在它上面)。
而不是将会话声明为:
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 无效...