Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the gRPC transport from NestJS microservices.
NestJS
import { [1] } from '@nestjs/microservices';
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing GrpcTransport with GrpcOptions.
Using GrpcService which does not exist.
Importing GrpcClient instead of GrpcOptions.
✗ Incorrect
The correct import for gRPC transport options in NestJS microservices is GrpcOptions.
2fill in blank
mediumComplete the code to set the transport type to gRPC in the microservice options.
NestJS
const grpcOptions = {
transport: [1],
options: {
package: 'hero',
protoPath: 'hero.proto'
}
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Transport.HTTP instead of Transport.GRPC.
Setting transport to Transport.TCP or Transport.REDIS by mistake.
✗ Incorrect
To use gRPC transport, set transport to Transport.GRPC in the options.
3fill in blank
hardFix the error in the code to correctly create a gRPC microservice.
NestJS
const app = await NestFactory.createMicroservice(AppModule, {
transport: Transport.GRPC,
options: {
package: 'hero',
protoPath: [1]
}
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting './' causes the file not to be found.
Using incorrect folder paths that don't match the project structure.
✗ Incorrect
The protoPath should be a relative path string starting with './' to correctly locate the proto file.
4fill in blank
hardFill both blanks to define a gRPC service client in a NestJS module.
NestJS
imports: [
ClientsModule.register([
{
name: '[1]',
transport: Transport.GRPC,
options: {
package: '[2]',
protoPath: './hero.proto'
}
}
])
] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up client name and package name.
Using incorrect package names that don't match proto.
✗ Incorrect
The client name is usually a service identifier like 'HERO_SERVICE', and the package name matches the proto package 'hero'.
5fill in blank
hardFill all three blanks to implement a gRPC method handler in a NestJS controller.
NestJS
@Controller() export class HeroController { @GrpcMethod('[1]', '[2]') findOne(data: { id: number }, metadata: any): [3] { return { id: data.id, name: 'HeroName' }; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect service or method names in the decorator.
Returning wrong types that don't match proto messages.
✗ Incorrect
The GrpcMethod decorator takes the service name and method name. The return type is the message type, here 'Hero'.