0
0
NestJSframework~10 mins

Hybrid applications in NestJS - Step-by-Step Execution

Choose your learning style9 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.