NEST JS从内存商店获取会话数据

发布于 2025-01-25 18:02:16 字数 82 浏览 4 评论 0原文

我想知道是否有一种方法可以直接访问Session Memory Store,并获取我使用Express-Session软件包的特定会话ID的会话数据。

I was wondering if there is a method to access directly the session memory store and get the session data for a particular session id i'm using express-session package with the nest js freamwork.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荆棘i 2025-02-01 18:02:16

希望您从那以后找到了问题的答案。我想到了使用Nestjs DI系统的以下技术。


背景

  • 根据Nestjs官方会话文​​档您可以在您的内注册一个会话中间软件main.ts文件。
  • 根据Expressjs官方的说法, session middleware文档 在页面顶部),您可以指定中间软件将使用的会话存储。如果省略了,则使用默认的内存存储。

所有会话存储都共享一个共同的接口,因此它是应用程序的可互换部分,这使得它们可以由模块作为注射令牌提供。请参阅 list> list express express商店的session商店。


实现详细信息

创建一个新的模块(例如SessionModule),然后将其添加到AppModule中的导入数组中。

接下来,我们为会话商店创建注射令牌。

import { MemoryStore } from 'express-session';
import { SESSION_STORE } from './session.const';

@Module({
   providers: [
      {
         provide: SESSION_STORE,
         useFactory: () => {
            return new MemoryStore();
         },
      },
   ],
   exports: [SESSION_STORE],
})
export class SessionModule {}

where session.const.ts现在在main.ts文件中包含一个简单的导出语句

export const SESSION_STORE = 'SESSION_STORE';

,您可以获取对Session Store的引用到SETUP SESICS MIDDERWARE。

const store = app.get(SESSION_STORE)
app.use(express.session({
    store,
    // ... other options
}
));

如果要在其他服务 /控制器中访问SessionStore,则必须将SessionModule导入声明它们的模块。

前任。

@Module({
   imports: [SessionModule],
   controllers: [UserController]
})
export class UserModule {}

// inside user controller you can inject a reference
// to the session store in the constructor
@Controller('user')
export class UserController {
    constructor(@Inject(SESSION_STORE) private sessionStore) {}
}

借助SessionStore属性,您可以获取任何存储的会话,其中包含链接的ExpressJS文档中记录的以下方法。 store.get(SID,回调)

Hope you found answers to your question since then. I came up with the following technique which is using NestJS DI system.


Background

  • According to NestJS official session documentation you can register a session middleware inside your main.ts file.
  • According to ExpressJS official session middleware documentation (the session(options) on top of the page), you can specify the session store that will be used by the middleware. If its omitted then the default MemoryStore is used.

All the session stores shares a common interface, so its a interchangeable part of your application, which makes a good candidate them to be provided as an injection token by a module. See a list of session stores advised by expressjs.


Implementation details

Create a new module (e.g. SessionModule), then add it to the imports array in your AppModule.

Next we create the injection token for the session store.

import { MemoryStore } from 'express-session';
import { SESSION_STORE } from './session.const';

@Module({
   providers: [
      {
         provide: SESSION_STORE,
         useFactory: () => {
            return new MemoryStore();
         },
      },
   ],
   exports: [SESSION_STORE],
})
export class SessionModule {}

where session.const.ts contains a simple export statement

export const SESSION_STORE = 'SESSION_STORE';

Now inside main.ts file, you can get a reference to the session store to setup session middleware.

const store = app.get(SESSION_STORE)
app.use(express.session({
    store,
    // ... other options
}
));

If you want to access sessionStore in other services / controllers you have to import the SessionModule to the module that declares them.

ex.

@Module({
   imports: [SessionModule],
   controllers: [UserController]
})
export class UserModule {}

// inside user controller you can inject a reference
// to the session store in the constructor
@Controller('user')
export class UserController {
    constructor(@Inject(SESSION_STORE) private sessionStore) {}
}

With the sessionStore property you can get any stored session with the following method documented in the linked expressjs documentation. store.get(sid, callback)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文