如何挂钩到快递会话创建事件

时间:2022-09-02 23:55:50

How can I tell when express creates a new session? I'm using a mongodb session store.

如何表达express创建新会话的时间?我正在使用mongodb会话商店。

I'm having an issue with extra sessions being created, and want to narrow down on the problem by tracking which urls requests are triggering new sessions to be created.

我遇到了创建额外会话的问题,并希望通过跟踪哪些网址请求触发要创建的新会话来缩小问题范围。

2 个解决方案

#1


2  

This is what I ended up doing.

这就是我最终做的事情。

app.use(function(req, res, next) {
    if (!req.session.returning) {
        // session was just created
        req.session.returning = true
    } else {
        // old session
    }
    next()
})

#2


1  

You can hook into the genid callback. By default express-session generates its own session IDs.

你可以挂钩genid回调。默认情况下,express-session会生成自己的会话ID。

But you can generate the session ID yourself by specifying a genid callback. Within this callback you can log that a new session is being created (and then return a unique session ID).

但是您可以通过指定genid回调来自己生成会话ID。在此回调中,您可以记录正在创建新会话(然后返回唯一的会话ID)。

Here is an example, adapted from the express-session README:

这是一个例子,改编自快递会自述文件:

app.use(session({
  genid: function(req) {
    console.log('New session was created');
    return genuuid(); // generate a UUID somehow and return it
  },
  secret: 'keyboard cat'
}));

#1


2  

This is what I ended up doing.

这就是我最终做的事情。

app.use(function(req, res, next) {
    if (!req.session.returning) {
        // session was just created
        req.session.returning = true
    } else {
        // old session
    }
    next()
})

#2


1  

You can hook into the genid callback. By default express-session generates its own session IDs.

你可以挂钩genid回调。默认情况下,express-session会生成自己的会话ID。

But you can generate the session ID yourself by specifying a genid callback. Within this callback you can log that a new session is being created (and then return a unique session ID).

但是您可以通过指定genid回调来自己生成会话ID。在此回调中,您可以记录正在创建新会话(然后返回唯一的会话ID)。

Here is an example, adapted from the express-session README:

这是一个例子,改编自快递会自述文件:

app.use(session({
  genid: function(req) {
    console.log('New session was created');
    return genuuid(); // generate a UUID somehow and return it
  },
  secret: 'keyboard cat'
}));