繁体   English   中英

Nestjs拦截器没有被调用

[英]Nestjs Interceptor not being invoked

我将 NestJS 与无服务器应用程序(部署到 AWS Lambda)一起使用。 我现在需要使用中间件或拦截器,因为它们在嵌套中被调用,但我正在努力让它们工作。 根据文档,我已从使用NestFactory.createApplicationContext更改为NestFactory.create ,这就是使用增强器(例如拦截器)包装 Controller 方法的原因

我在一个模块中注册拦截器,所以它应该是全局可用的

const loggingInterceptorProvider = {
  provide: APP_INTERCEPTOR,
  useClass: LoggingInterceptor,
};

我的引导程序看起来像这样

export async function bootstrap(Module: any) {
  if (app) return app;
  app = await NestFactory.createApplicationContext(Module);
 return await app.init();
}

现在是非标准位,因为我使用的是通用“构建器”(库代码),构建器将 controller 名称作为字符串传递,然后调用它,因此

// the Module is accessible in the bootstrap via a closure, not shown in this code
const app = await bootstrap();
const appController = app.get(Controller);
// functionName is a string
const controllerFunction = appController[functionName];

const boundControllerFunction = controllerFunction.bind(
  appController,
);

const result = await boundControllerFunction(body);

我没有看到我的任何拦截器记录 output。 难道我做错了什么? 还是我调用不与拦截器一起使用的 Controller 的方式?

编辑:

为了完整起见,这是我使用的正确引导程序 function

let cachedApp: INestApplication;
export async function bootstrap(Module: any) {
  if (cachedApp) return cachedApp;

  cachedApp = await NestFactory.create(Module, {
    bufferLogs: true,
    logger: ['error', 'warn'],
  });

  await cachedApp.init();

  return cachedApp;
}

这是因为您直接调用了 controller 方法,绕过了 nestjs 生命周期。 当 Nest js 服务器处理请求时,它会应用其内部机制来运行拦截器、验证管道和异常过滤器。 如果直接调用 class 方法将不会被使用。

在您的情况下,您可以遵循nestjs文档的这一部分:

https://docs.nestjs.com/faq/serverless#example-integration

let server: Handler;

async function bootstrap(): Promise<Handler> {
  const app = await NestFactory.create(AppModule);
  await app.init();

  const expressApp = app.getHttpAdapter().getInstance();
  return serverlessExpress({ app: expressApp });
}

export const handler: Handler = async (
  event: any,
  context: Context,
  callback: Callback,
) => {
  server = server ?? (await bootstrap());
  return server(event, context, callback);
};

如果您想调用一些服务代码,而不是 controller,则文档中的“独立应用程序功能”非常有用。


顺便说一句,在代码片段中,您可以看到变量server ,他们故意将其移到处理程序 function 之外。 因为在 AWS lambdas 中,它可以在不同的请求之间缓存。

我找到了一种方法来做到这一点,使用记录很差的功能ExternalContextCreator 所以基本上我上面发布的最后一个代码片段会变成这个

  import { ExternalContextCreator } from '@nestjs/core/helpers/external-context-creator';   

  // the Module is accessible in the bootstrap via a closure, not shown in this code
  const app = await bootstrap();
  const appController = app.get(Controller);
  // functionName is a string
  const controllerFunction = appController[functionName];

  const extContextCreator = app.get(ExternalContextCreator);

  const boundControllerFunction = extContextCreator.create(
    appController,
    controllerFunction,
    String(functionName),
  );

  const result = await boundControllerFunction(body);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM