会话未使用Express-Session持久化

时间:2019-09-25 19:17:09

标签: node.js express session cookies express-session

另一个关于会话没有持续的问题。...

我正在使用快递和路由控制器来设置我的路线。

我有一个会话中间件:

@Middleware({ type: 'before' })
export class SessionMiddleware implements ExpressMiddlewareInterface {

    public use(req: express.Request, res: express.Response, next: express.NextFunction): any {
        var session = require('express-session');
        var sess = {
            name:'some_session',
            secret: 'lalala',
            resave: true,
            saveUninitialized: true
        }
        return session(sess)(req, res, next);
    }

}

我有一个/api/onboarding路线的控制器

 @Get()
    public async createGet(@Res() res: any, @Req() req: any) : Promise<Response> {
        console.log("Session in: " + req.session.id +" "+ JSON.stringify(req.session.userId));
        req.session.userId = 'userID';
        console.log("Session out: "+ req.session.id +" " + JSON.stringify(req.session.userId));
        return res.send(req.session.userId);
    }

正在响应中发送cookie:

通过邮递员首次致电获取http://localhost/api/onboarding

Session in: EQQoZ9oC6Y68OcI9xqvQ7bCTEMFb8T1Q undefined
Session out: EQQoZ9oC6Y68OcI9xqvQ7bCTEMFb8T1Q "userID"

邮递员获取cookie。

第二次调用带有邮递员和cookie的Get http://localhost/api/onboarding

Session in: 6iZFSdFvlpP4r1Pj7m3Q2j0GKGenSHNZ undefined
Session out: 6iZFSdFvlpP4r1Pj7m3Q2j0GKGenSHNZ "userID"

我希望在Session in看到“ userID”,但看起来express生成了一个新会话,并且不能正确读取发送的cookie。

我可以看到在Postman中,cookie设置正确,并在第二个请求中发送了。我想念什么?

0 个答案:

没有答案