Consider a NestJS microservice configured with Redis transport that listens for a message pattern 'sum'. The handler adds two numbers from the data and returns the result.
What will be the response when the client sends {a: 5, b: 7}?
import { Controller } from '@nestjs/common'; import { MessagePattern } from '@nestjs/microservices'; @Controller() export class MathController { @MessagePattern('sum') sum(data: {a: number; b: number}): number { return data.a + data.b; } }
Think about what the sum function returns when given two numbers.
The handler receives the data object with properties a and b. It returns their sum, so 5 + 7 equals 12.
Choose the correct way to create a NestJS microservice using Redis transport on port 6379.
import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { Transport } from '@nestjs/microservices'; async function bootstrap() { const app = await NestFactory.createMicroservice(AppModule, { transport: Transport.REDIS, options: { host: 'localhost', port: 6379, }, }); await app.listen(); } bootstrap();
NestJS Redis transport supports host and port in options.
Option A correctly uses Transport.REDIS and provides host and port, matching the official NestJS documentation for Redis transport.
Given the following microservice bootstrap code, why does the connection to Redis fail?
const app = await NestFactory.createMicroservice(AppModule, { transport: Transport.REDIS, options: { host: '127.0.0.1', port: 6380, }, }); await app.listen();
Check if Redis is running on the specified port.
The code tries to connect to Redis on port 6380, but Redis usually runs on 6379 by default. If Redis is not running on 6380, connection fails.
In a NestJS client, a message with pattern 'multiply' is sent with data {x: 3, y: 4}. The microservice multiplies these numbers and returns the result.
What is the value of 'response' after awaiting the send call?
const client = app.connectMicroservice({
transport: Transport.REDIS,
options: { url: 'redis://localhost:6379' },
});
await client.connect();
const response = await client.send('multiply', { x: 3, y: 4 }).toPromise();Think about what the microservice returns for the 'multiply' pattern.
The microservice multiplies x and y, so 3 * 4 equals 12. The client receives this value as the response.
Choose the correct statement about using Redis transport in NestJS microservices.
Consider how NestJS microservices handle messaging with Redis transport.
Redis transport in NestJS supports request-response messaging with automatic serialization and pattern-based routing, making option D true.