Bird
Raised Fist0
NestJSframework~10 mins

Hybrid applications in NestJS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Hybrid applications
Start NestJS App
↓
Create HTTP Server
↓
Create Microservice Server
↓
Combine Servers in Hybrid App
↓
Listen to HTTP Requests
↓
Listen to Microservice Messages
↓
Handle Requests & Messages
↓
Shutdown Servers
The NestJS hybrid app starts by creating both HTTP and microservice servers, then combines them to listen and handle requests simultaneously.
Execution Sample
NestJS
import { NestFactory } from '@nestjs/core';
import { Transport } from '@nestjs/microservices';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.connectMicroservice({ transport: Transport.TCP });
  await app.startAllMicroservices();
  await app.listen(3000);
}

bootstrap();
This code creates a NestJS app that runs both an HTTP server and a TCP microservice server together.
Execution Table
StepActionEvaluationResult
1Call NestFactory.create(AppModule)Creates HTTP app instanceHTTP server ready
2Call app.connectMicroservice({ transport: Transport.TCP })Adds microservice configMicroservice server configured
3Call app.startAllMicroservices()Starts microservice serverMicroservice server listening
4Call app.listen(3000)Starts HTTP server on port 3000HTTP server listening
5Receive HTTP requestRoute to HTTP handlerResponse sent
6Receive microservice messageRoute to microservice handlerMessage processed
7Shutdown appClose HTTP and microservice serversServers stopped
💡 Execution stops when servers are shut down or app is terminated
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
appundefinedHTTP app instanceHTTP + microservice configMicroservice startedHTTP + microservice listeningServers stopped
Key Moments - 3 Insights
Why do we call connectMicroservice before starting microservices?
Because connectMicroservice sets up the microservice configuration needed before starting it, as shown in steps 2 and 3 of the execution_table.
Does app.listen start both HTTP and microservice servers?
No, app.listen only starts the HTTP server (step 4). Microservices start separately with startAllMicroservices (step 3).
Can the app handle HTTP and microservice requests at the same time?
Yes, after both servers start (steps 3 and 4), the app listens and handles both kinds of requests concurrently (steps 5 and 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of 'app' after step 2?
AHTTP app instance only
BHTTP app with microservice configured
CMicroservice started
DServers stopped
💡 Hint
Check variable_tracker column 'After Step 2' for 'app' state.
At which step does the HTTP server start listening on port 3000?
AStep 1
BStep 3
CStep 4
DStep 2
💡 Hint
Look at execution_table row with action 'Call app.listen(3000)'.
If we skip startAllMicroservices(), what happens?
AMicroservice server will not start listening
BHTTP server will not start
CBoth servers start normally
DApp will crash immediately
💡 Hint
Refer to execution_table steps 3 and 4 about starting servers.
Concept Snapshot
NestJS Hybrid Applications:
- Create HTTP app with NestFactory.create()
- Add microservice with connectMicroservice()
- Start microservices with startAllMicroservices()
- Start HTTP server with listen(port)
- Both servers run concurrently handling requests
- Shutdown closes all servers cleanly
Full Transcript
This visual trace shows how a NestJS hybrid application runs. First, the app is created as an HTTP server. Then, a microservice is connected and started separately. After both servers are running, the app listens for HTTP requests on port 3000 and microservice messages simultaneously. The variable 'app' changes from undefined to an HTTP app instance, then to a hybrid app with microservice configured and started. Key moments clarify why microservices start separately and how both servers handle requests concurrently. The quiz tests understanding of app state after each step and the order of server startup. This helps beginners see the step-by-step flow of hybrid app execution in NestJS.

Practice

(1/5)
1. What is a hybrid application in NestJS?
easy
A. An app that combines HTTP and microservices in one project
B. An app that only uses HTTP controllers
C. An app that only uses microservices
D. An app that runs on multiple servers

Solution

  1. Step 1: Understand NestJS application types

    NestJS apps can be HTTP-based, microservice-based, or both combined.
  2. Step 2: Define hybrid application

    A hybrid app combines HTTP and microservices in one project to handle both communication types.
  3. Final Answer:

    An app that combines HTTP and microservices in one project -> Option A
  4. Quick Check:

    Hybrid app = HTTP + microservices [OK]
Hint: Hybrid means mixing HTTP and microservices in one app [OK]
Common Mistakes:
  • Thinking hybrid means multiple servers
  • Confusing hybrid with only HTTP or only microservices
  • Assuming hybrid apps are separate projects
2. Which method is used to add microservice support in a NestJS hybrid application?
easy
A. connectMicroservice()
B. createMicroservice()
C. addMicroservice()
D. useMicroservice()

Solution

  1. Step 1: Recall NestJS microservice integration method

    The official NestJS method to add microservices to an app is connectMicroservice().
  2. Step 2: Verify method names

    Other options like createMicroservice(), addMicroservice(), or useMicroservice() are not valid NestJS methods.
  3. Final Answer:

    connectMicroservice() -> Option A
  4. Quick Check:

    Use connectMicroservice() to add microservices [OK]
Hint: Remember: connectMicroservice() connects microservices [OK]
Common Mistakes:
  • Using createMicroservice() which does not exist
  • Confusing with addMicroservice() or useMicroservice()
  • Trying to add microservices without this method
3. Given this NestJS hybrid app code snippet, what will be the output when the app starts?
const app = await NestFactory.create(AppModule);
app.connectMicroservice({ transport: Transport.TCP });
await app.startAllMicroservices();
await app.listen(3000);
console.log('App is running');
medium
A. App listens on port 3000 but microservices do not start
B. App is running (and microservices start listening)
C. Error: connectMicroservice is not a function
D. App crashes due to missing microservice configuration

Solution

  1. Step 1: Analyze microservice connection

    The code calls connectMicroservice with TCP transport, which is valid to add microservices.
  2. Step 2: Check microservice start and app listen

    startAllMicroservices() starts microservices, then app.listen(3000) starts HTTP server. Console logs 'App is running'.
  3. Final Answer:

    App is running (and microservices start listening) -> Option B
  4. Quick Check:

    connectMicroservice + startAllMicroservices + listen = running app [OK]
Hint: connectMicroservice + startAllMicroservices means microservices run [OK]
Common Mistakes:
  • Thinking microservices start automatically without startAllMicroservices()
  • Confusing connectMicroservice with createMicroservice
  • Assuming app.listen blocks microservices
4. Identify the error in this NestJS hybrid app setup:
const app = await NestFactory.create(AppModule);
app.connectMicroservice({ transport: Transport.REDIS });
await app.listen(3000);
await app.startAllMicroservices();
medium
A. AppModule cannot be used with microservices
B. connectMicroservice() does not accept transport option
C. startAllMicroservices() must be called before app.listen()
D. Missing await before connectMicroservice()

Solution

  1. Step 1: Check method call order

    In NestJS, microservices must be started before the HTTP server listens, so startAllMicroservices() should be before app.listen().
  2. Step 2: Validate other options

    connectMicroservice() accepts transport option; AppModule supports microservices; connectMicroservice() is synchronous, no await needed.
  3. Final Answer:

    startAllMicroservices() must be called before app.listen() -> Option C
  4. Quick Check:

    Start microservices before HTTP listen [OK]
Hint: Call startAllMicroservices() before app.listen() [OK]
Common Mistakes:
  • Calling startAllMicroservices() after app.listen()
  • Adding await to connectMicroservice()
  • Thinking transport option is invalid
5. You want to build a NestJS hybrid app that handles HTTP requests and listens to RabbitMQ messages. Which steps correctly set up the microservice part?
hard
A. Use connectMicroservice({ transport: Transport.TCP }) and call startAllMicroservices() after app.listen()
B. Use createMicroservice({ transport: Transport.RMQ }) and call app.listen() before starting microservices
C. Add RabbitMQ config in AppModule imports and call app.listen() only
D. Use connectMicroservice({ transport: Transport.RMQ, options: { urls: ['amqp://localhost'], queue: 'main_queue' } }), then call startAllMicroservices() before app.listen()

Solution

  1. Step 1: Choose correct microservice setup for RabbitMQ

    connectMicroservice() with Transport.RMQ and proper options (urls, queue) is the correct way to add RabbitMQ microservice.
  2. Step 2: Call startAllMicroservices() before app.listen()

    Microservices must start before HTTP server listens to ensure both run properly.
  3. Final Answer:

    Use connectMicroservice({ transport: Transport.RMQ, options: { urls: ['amqp://localhost'], queue: 'main_queue' } }), then call startAllMicroservices() before app.listen() -> Option D
  4. Quick Check:

    connectMicroservice + RMQ options + startAllMicroservices before listen [OK]
Hint: Set RMQ options in connectMicroservice and start microservices first [OK]
Common Mistakes:
  • Using createMicroservice() which is not a NestFactory method
  • Calling app.listen() before starting microservices
  • Using wrong transport type for RabbitMQ
  • Not providing RabbitMQ connection options
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard