Seth
Seth

Reputation: 145

NestJS gRPC Middlware

Does NestJS support middleware with gRPC? I'm following the example project here and then the middleware for logging the request entrypoint here.

In the example project it looks like there is an Express server along with the gRPC server. I'm using just a gRPC server.

const app = await NestFactory.createMicroservice<MicroserviceOptions>(...);
await app.listenAsync();

So adding the following to the main app module:

export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(LoggerMiddleware)
      .forRoutes('*');
  }
}

But nothing is logging.

Upvotes: 3

Views: 1925

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70061

Middleware is exclusive to HTTP handlers. If you need middleware-like functionality, it would be better to use one of Nest's enhancers (guards, interceptors, pipes, or filters). If you're looking to do some logging I'd suggest an intereptor as you have pre- and post- controller logic.

There's also my logging library Ogma which has a Nest package and interceptor already which might be useful to look at

Upvotes: 5

Related Questions