0
0
NestJSframework~20 mins

Hybrid applications in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS Hybrid Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when running a NestJS hybrid app with both HTTP and microservice listeners?

Consider a NestJS hybrid application that starts both an HTTP server and a microservice listener. What will happen when you send an HTTP request to the app's endpoint?

NestJS
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { Transport } from '@nestjs/microservices';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.connectMicroservice({ transport: Transport.TCP });
  await app.startAllMicroservices();
  await app.listen(3000);
}
bootstrap();
AThe app listens on port 3000 for HTTP requests and also listens for TCP microservice messages simultaneously.
BThe app only listens for TCP microservice messages and ignores HTTP requests.
CThe app listens only on port 3000 for HTTP requests and does not start the microservice listener.
DThe app throws an error because it cannot start both HTTP and microservice listeners at the same time.
Attempts:
2 left
💡 Hint

Think about how NestJS handles multiple listeners in a hybrid app.

lifecycle
intermediate
2:00remaining
What happens if you call app.listen() before app.startAllMicroservices() in a NestJS hybrid app?

In a NestJS hybrid app, you have two ways to start servers: app.listen() for HTTP and app.startAllMicroservices() for microservices. What is the effect of calling app.listen() before app.startAllMicroservices()?

NestJS
async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.connectMicroservice({ transport: Transport.REDIS });
  await app.listen(3000);
  await app.startAllMicroservices();
}
bootstrap();
AOnly the HTTP server runs; the microservice listener is ignored silently.
BThe microservice listener never starts because app.listen() blocks the event loop.
CThe app throws an error because startAllMicroservices must be called before listen.
DThe HTTP server starts first, then the microservice listener starts after; both run correctly.
Attempts:
2 left
💡 Hint

Consider how async/await works and how NestJS manages multiple listeners.

📝 Syntax
advanced
2:00remaining
Which option correctly creates a NestJS hybrid app with HTTP and RabbitMQ microservice?

Choose the code snippet that correctly sets up a NestJS hybrid application with an HTTP server and a RabbitMQ microservice listener.

A
const app = await NestFactory.create(AppModule);
app.connectMicroservice({ transport: Transport.RMQ, options: { urls: 'amqp://localhost', queue: 'main_queue' } });
await app.startAllMicroservices();
await app.listen(3000);
B
const app = await NestFactory.create(AppModule);
app.connectMicroservice({ transport: Transport.RMQ, options: { urls: ['amqp://localhost'], queue: 'main_queue' } });
await app.startAllMicroservices();
await app.listen(3000);
C
const app = await NestFactory.create(AppModule);
app.connectMicroservice({ transport: Transport.RMQ, options: { url: 'amqp://localhost', queue: 'main_queue' } });
await app.startAllMicroservices();
await app.listen(3000);
D
const app = await NestFactory.create(AppModule);
app.connectMicroservice({ transport: Transport.RMQ, options: { url: ['amqp://localhost'], queue: 'main_queue' } });
await app.startAllMicroservices();
await app.listen(3000);
Attempts:
2 left
💡 Hint

Check the property name for RabbitMQ connection URLs in NestJS microservice options.

🔧 Debug
advanced
2:00remaining
Why does this NestJS hybrid app fail to receive microservice messages?

Given the following code, why does the microservice listener not receive any messages?

NestJS
async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.connectMicroservice({ transport: Transport.TCP });
  await app.listen(3000);
  // Missing await app.startAllMicroservices();
}
bootstrap();
ABecause the transport type TCP is not supported in hybrid apps.
BBecause <code>app.listen()</code> blocks microservice messages from being processed.
CBecause <code>app.startAllMicroservices()</code> was not called, so the microservice listener never started.
DBecause the microservice listener must be started before <code>app.listen()</code> or it fails silently.
Attempts:
2 left
💡 Hint

Check if the microservice listener is actually started.

🧠 Conceptual
expert
2:00remaining
What is the main advantage of using a NestJS hybrid application?

Why would a developer choose to build a NestJS hybrid application instead of separate HTTP and microservice apps?

AIt allows sharing the same codebase and dependency injection container for both HTTP and microservice servers, reducing duplication and improving maintainability.
BIt automatically scales microservices independently without any additional configuration.
CIt forces the app to use only one transport protocol, simplifying network setup.
DIt disables HTTP server features to optimize microservice performance.
Attempts:
2 left
💡 Hint

Think about code reuse and architecture benefits.