title: How can a NestJS app listen on the same port as a connected microservice?
body: <p>I'm trying to understand how Nest microservices work.</p>
  <p>Let's start with this example from the docs (<a href="https://docs.nestjs.com/faq/hybrid-application" rel="noreferrer">https://docs.nestjs.com/faq/hybrid-application</a>)</p>
  <pre><code>const app = await NestFactory.create(AppModule);
  // microservice #1
  const microserviceTcp = app.connectMicroservice&lt;MicroserviceOptions&gt;({
    transport: Transport.TCP,
    options: {
      port: 3001,
    },
  });
  // microservice #2
  const microserviceRedis = app.connectMicroservice&lt;MicroserviceOptions&gt;({
    transport: Transport.REDIS,
    options: {
      url: 'redis://localhost:6379',
    },
  });
  
  await app.startAllMicroservicesAsync();
  await app.listen(3001);
  </code></pre>
  <p>After going through the source (<a href="https://github.com/nestjs/nest" rel="noreferrer">https://github.com/nestjs/nest</a>) , I understand <code>connectMicroservice</code> creates a <code>net.Server</code> when using <code>TCP</code> and the server starts listening when <code>startAllMicroservicesAsync</code> is called.
  But then <code>app.listen</code> should initialize the listening of the base Nest webserver.</p>
  <p>Why doesn't that cause an error?</p>
  <p>I checked what happens if I have two microservices connected on the port. It sure does throw an error. What am I missing here?</p>
  <pre><code>// CODE CAUSES ERROR AS EXPECTED
  
  const app = await NestFactory.create(AppModule);
  // microservice #1
  const microserviceTcp = app.connectMicroservice&lt;MicroserviceOptions&gt;({
    transport: Transport.TCP,
    options: {
      port: 3001,
    },
  });
  
  // microservice #2  &lt;--- THIS WILL CAUSE AN ERROR AS EXPECTED
  const microserviceTcp = app.connectMicroservice&lt;MicroserviceOptions&gt;({
    transport: Transport.TCP,
    options: {
      port: 3001,
    },
  });
  
  await app.startAllMicroservicesAsync();
  await app.listen(3001);
  </code></pre>