我在 Nestjs 中实现了一个 TCP 微服务,试图遵循文档,我正在努力测试该应用程序,我在 google PacketSender 上找到了这个应用程序,它们向我证明该请求正常工作,因为我登录了提示有关请求,但我没有收到 ASCII 信息,有人知道我错过了什么吗?
main.ts
async function bootstrap() {
const config = new ConfigService().get();
const app = await NestFactory.createMicroservice(MainModule, {
transport: config.authMS.transport,
options: {
host: config.authMS.options.host,
port: Number(config.authMS.options.port),
},
} as TcpOptions);
await app.listen();
}
bootstrap();
main.module.ts
@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
TypeOrmModule.forRoot(configMainModulesService.getTypeOrmConfig()),
AuthModule,
],
})
export class MainModule {}
auth.controller.ts
@MessagePattern('getUser')
async user(data: { login: string; token: string }): Promise<any> {
const res = await this.authService.getUser(data);
console.log('ress =>', res);
return res;
}
auth.service.ts
public async getUser(data: any): Promise<any> {
const tokenExists = await this.authRepository.findOne({
where: { token: data.token },
});
if (tokenExists) {
const user = await this.authRepository.findOne({
where: { login: data.login },
});
if (!user) {
return {
status: HttpStatus.NOT_FOUND,
message: 'user_search_by_credentials_not_found',
user: null,
};
}
return {
status: HttpStatus.OK,
message: 'user_search_by_credentials_success',
user: this.toPublicUser(user),
};
} else {
return {
status: HttpStatus.BAD_REQUEST,
message: 'user_search_by_credentials_token_invalid',
user: null,
};
}
}
我也遇到了同样的问题,最后我成功了!
您需要将“id”键添加到您的请求 ASCII 字符串中
61#{"pattern":"sum","id":"ce51ebd3-32b1-4ae6-b7ef-e018126c4cc4"}