nestjs 中间件测试不触发next会如何

317 阅读1分钟

中间件套路


@Injectable()
class zhongjianjian implements NestMiddleware{

use(req: Request, res: Response, next: NextFunction) {
    console.log('Request...');
    //next(); //测试如这个没有触发会如何? 是不是定制运行
    throw new Error('我是异常')
  }



}

使用
export class Module  implements Nestmodues{
  configure(consumer:MiddlewareConsumer){
     consumer
          .apply(LoggerMiddleware)
          .forRoutes('*');
 }
}

测试时候发现 next()没有执行,请求直接挂起了 在测试一个 中间件抛异常如何?


起码没有挂起异常,发送了一个错误的请求回来,挺好