代码之家  ›  专栏  ›  技术社区  ›  Guerric P

如何在环回4中使用有状态请求?

  •  2
  • Guerric P  · 技术社区  · 6 年前

    在v3中,我们可以在底层Express上定义中间件,比如 express-session session 请求对象的属性。

    在v4中,似乎没有对应的版本,底层的Express也没有公开。

    我正在将身份验证提供程序从v3迁移到v4,因此我陷入了困境。我该怎么办?

    import { MyApplication } from './application';
    import { ApplicationConfig } from '@loopback/core';
    
    const expressSession = require('express-session');
    
    export { MyApplication };
    
    export async function main(options: ApplicationConfig = {}) {
      const app = new MyApplication (options);
      // @ts-ignore
      app.restServer._expressApp.use(expressSession);
      await app.boot();
      await app.start();
    
      const url = app.restServer.url;
      console.log(`Server is running at ${url}`);
      console.log(`Try ${url}/ping`);
    
      return app;
    }
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   Guerric P    6 年前

    环回团队的您好:)

    Sequence docs .

    class MySequence {
      // skipping constructor with dependencies to be injected
    
      async handle(context: RequestContext) {
        try {
          // the following line is new
          const session = this.restoreSession(context);
    
          // the default sequence continues here
          const route = this.findRoute(context.request);
          const params = await this.parseParams(context.request, route);
          const result = await this.invoke(route, params);
          await this.send(context.response, result);
        } catch (error) {
          await this.reject(context, error);
        }
      }
    }
    

    顺序动作 restoreSession

    • 检查请求中是否存在会话cookie
    • 如果提供了cookie,则加载会话,否则可能创建新会话?
    • Context 这样其他地方比如控制器可以通过 Dependency Injection

    请在中打开一个新的GitHub问题(一个功能请求)好吗 loopback-next