Closed aasailan closed 2 years ago
All what @Session
do is:
case "session":
if (param.name)
return request.session[param.name];
return request.session;
when i check my redis db, the user info is not save in th session
Please show more code, how you bootstrap the app, configure redis session, etc.
thanks for your reply and this is how I bootstrap the app
import 'reflect-metadata';
import * as express from 'express';
import * as path from 'path';
import * as cookieParser from 'cookie-parser';
import * as bodyParser from 'body-parser';
import * as log4js from 'log4js';
import * as session from 'express-session';
import * as connectRedis from 'connect-redis'; // 针对express-session 和 redis存储的中间件
import { useExpressServer, useContainer } from 'routing-controllers';
import { Container } from 'typedi';
import { logger, sessionConfig, redisConfig } from './config/index';
const app = express();
const RedisStore = connectRedis(session);
sessionConfig.store = new RedisStore({
host: '127.0.0.1',
port: 6379,
ttl: 86400, // 会话保存在redis的时间,单位s
db: 4,
pass: '',
prefix: 'typescript-blog-2.0',
logErrors: true
});
// 视图设置
app.set('views', path.resolve( 'built', 'client', 'views'));
app.set('view engine', 'jade');
// 挂载中间件
app.use(log4js.connectLogger(logger, {}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(session({
secret: 'peishan',
name: 'blog.sid', // cookie name
resave: true, // resave==true的时候,强迫每次链接都会重新设置会话的有效期。为什么不设置为true
cookie: { // cookie 内容设置
maxAge: 1000 * 60 * 24 // 一天过期 单位ms
},
saveUninitialized: true // stored 设置为true则,每次都为一个为初始化的会话创建一个会话对象
}));
app.use(express.static(path.resolve('built', 'client', 'public')));
useContainer(Container); // 需要设置Container 才能在Controller里面进行注入
const controllerPath = path.resolve('built', 'server', 'controllers', '*.js');
const middlewarePath = path.resolve('built', 'server', 'middlewares', '*.js');
logger.debug('controllerPath', controllerPath);
logger.debug('middlewarePath', middlewarePath);
useExpressServer(app, {
controllers: [controllerPath],
middlewares: [middlewarePath],
defaultErrorHandler: false
});
export default app;
@19majkel94 I am so sorry, my English is not good,(⁎˃ᴗ˂⁎)
Stale issue message
@aasailan This seems to be an issue with express-session itself. The same happens even if you do the following:
myRoute(@Req() req) {
const session = req.session.
session.regenerate(() => {
session.myVar = 'test';
})
}
So after you assign session to a variable and use regenerate you lose the ability to set session variables.
Closing as stale
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
I try to svae user info into session , and the code like this:
however, when i check my redis db, the user info is not save in th session. but when i use req,session to save the user info, it work, the code like this: