Closed unlight closed 6 years ago
Looks like examples outdated... Example in docs
async function bootstrap() {
const app = await NestFactory.createMicroservice(ApplicationModule, {
transport: Transport.TCP,
});
app.useGlobalInterceptors(new LoggingInterceptor());
app.listen(() => console.log('Microservice is listening'));
}
bootstrap();
Work as expected.
Hi @unlight, Third example concerns hybrid app (HTTP server + microservice(s) https://docs.nestjs.com/faq/hybrid-application)
So, then the question is why global interceptors (filters, etc.) for microservices are not working in hybrid app? Is it by design?
Because in this case, you bind global interceptors etc. to the HTTP app instead of microservice
Let's take a look at code (1st message):
async function bootstrap() {
const app = await NestFactory.create(ApplicationModule); // http app
const microservice = app.connectMicroservice({ transport: Transport.TCP, port: 43210 });
microservice.useGlobalInterceptors(new LoggingInterceptor()); // <-- `microservice` here is also http app?
await app.startAllMicroservicesAsync();
await app.listen(3000);
}
bootstrap();
@kamilmysliwiec Are you saying, that:
Line 2: app
is a http app. Correct?
Line 3: microservice: INestMicroservice
is also http app?
The reason is that when you use hybrid application, each connected microservice has to be initialized immediately, and thus you can't add global interceptors(or anything) dynamically. There's no API to do this at the moment, you have to use a dedicated function - createMicroservice()
.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Message in gitter: https://gitter.im/nestjs/nestjs?at=5a5a5e1c5a9ebe4f75824518
Question: So, then the question is why global intercepts (filters, etc.) for microservices are not working in hybrid app?
I'm submitting a...
Current behavior
Code was taken from examples folder. Microservice global interceptors does not work (or not applicable)
Expected behavior
Global interceptors in microservices should work as in regular web app.
Minimal reproduction of the problem with instructions
Result:
No messages from logger interceptors.
Environment