nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.9k stars 7.55k forks source link

examples/03-microservices: hybrid apps - global interceptors does not work in microservice #353

Closed unlight closed 6 years ago

unlight commented 6 years ago

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...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Code was taken from examples folder. Microservice global interceptors does not work (or not applicable)

async function bootstrap() {
    const app = await NestFactory.create(ApplicationModule);
    const microservice = app.connectMicroservice({ transport: Transport.TCP, port: 43210 });
    microservice.useGlobalInterceptors(new LoggingInterceptor()); // BUG?
    await app.startAllMicroservicesAsync();
    await app.listen(3000);
}
bootstrap();

Expected behavior

Global interceptors in microservices should work as in regular web app.

Minimal reproduction of the problem with instructions

  1. Repo https://github.com/unlight/nest-typescript-starter/tree/bug_2018_1
  2. git clone https://github.com/unlight/nest-typescript-starter
  3. cd nest-typescript-starter
  4. git checkout bug_2018_1
  5. npm install
  6. cd app/hybrid-example
  7. npx ts-node server.ts
  8. npx ts-node client.ts (or open http://localhost:3000/hello)

Result:

Service 1: got  { a: 1, b: 2 }
Service 2: got  hello from service 1
Service 1: reply from service2 done from service 2

No messages from logger interceptors.

Environment


Nest version: @nestjs/core@4.5.10

For Tooling issues:
- Node version: XX  v8.9.0
- Platform:  Windows 8
unlight commented 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.

kamilmysliwiec commented 6 years ago

Hi @unlight, Third example concerns hybrid app (HTTP server + microservice(s) https://docs.nestjs.com/faq/hybrid-application)

unlight commented 6 years ago

So, then the question is why global interceptors (filters, etc.) for microservices are not working in hybrid app? Is it by design?

kamilmysliwiec commented 6 years ago

Because in this case, you bind global interceptors etc. to the HTTP app instead of microservice

unlight commented 6 years ago

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?

kamilmysliwiec commented 6 years ago

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().

lock[bot] commented 4 years ago

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.