0
0
NestJSframework~10 mins

Message patterns (request-response) in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a message pattern handler for 'get_data'.

NestJS
import { Controller } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';

@Controller()
export class DataController {
  @MessagePattern('[1]')
  getData() {
    return { data: 'Hello' };
  }
}
Drag options to blanks, or click blank then click option'
A'get_data'
B'fetch_data'
C'send_data'
D'update_data'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong or unrelated message pattern string.
Forgetting to put the pattern string inside quotes.
2fill in blank
medium

Complete the code to inject the ClientProxy and send a request with 'get_data' pattern.

NestJS
import { Controller, Inject } from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';

@Controller()
export class AppController {
  constructor(@Inject('[1]') private client: ClientProxy) {}

  async requestData() {
    return this.client.send('get_data', {}).toPromise();
  }
}
Drag options to blanks, or click blank then click option'
A'DATA_SERVICE'
B'MY_SERVICE'
C'CLIENT_PROXY'
D'SERVICE_CLIENT'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a token that was not registered in the module providers.
Forgetting to put the token string inside quotes.
3fill in blank
hard

Fix the error in the message handler to correctly receive data and return a response.

NestJS
import { Controller } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';

@Controller()
export class CalcController {
  @MessagePattern('sum')
  calculate([1]: number[]) {
    return data.reduce((a, b) => a + b, 0);
  }
}
Drag options to blanks, or click blank then click option'
Apayload
Bdata
Cnumbers
Dvalues
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name different from the variable used inside the method.
Forgetting to destructure or type the parameter correctly.
4fill in blank
hard

Fill both blanks to create a message pattern handler that returns the square of a number.

NestJS
import { Controller } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';

@Controller()
export class MathController {
  @MessagePattern([1])
  square([2]: number) {
    return num * num;
  }
}
Drag options to blanks, or click blank then click option'
A'square_number'
Bnum
Cvalue
D'calculate_square'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a pattern string that does not match the event.
Using a parameter name different from the variable used inside the method.
5fill in blank
hard

Fill all three blanks to send a request and handle the response with async/await.

NestJS
import { Controller, Inject } from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';

@Controller()
export class AppController {
  constructor(@Inject([1]) private client: ClientProxy) {}

  async getResult() {
    const response = await this.client.send([2], [3]).toPromise();
    return response;
  }
}
Drag options to blanks, or click blank then click option'
A'DATA_SERVICE'
B'get_data'
C{}
D'MY_SERVICE'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong injection token.
Sending a wrong or missing message pattern.
Forgetting to provide a payload object.