0

I’m building a monorepo using Turborepo and NestJS, following a microservices architecture with TCP communication between services.

Here’s my folder structure:

   apps/
 ├── api-gateway/
 │   ├── src/
 │   ├── package.json
 │   ├── tsconfig.json
 │   └── ...
 ├── auth-service/
 │   ├── src/
 │   ├── package.json
 │   ├── tsconfig.json
 │   └── ...
 └── notification-service/

I’m trying to make the api-gateway communicate with the auth-service via TCP, but I keep getting the following error on Insomnia:

{
  "statusCode": 500,
  "message": "Internal server error"
}

enter image description here

api-gateway/app.module.ts

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ClientsModule, Transport } from '@nestjs/microservices';
@Module({
  imports: [
    ClientsModule.register([
      {
        name: 'AUTH_SERVICE',
        transport: Transport.TCP,
        options: {
          host: '127.0.0.1',
          port: 3001,
        },
      },
    ]),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

auth-service/main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { Logger } from '@nestjs/common';

async function bootstrap() {
  const logger = new Logger('AuthService');
  console.log('ola');
  try {
    const app = await NestFactory.createMicroservice<MicroserviceOptions>(
      AppModule,
      {
        transport: Transport.TCP,
        options: {
          host: '127.0.0.1',
          port: 3001,
        },
      },
    );

    await app.listen();
    logger.log('Auth Service is listening on port 8877');
  } catch (error) {
    logger.error('Failed to start Auth Service', error);
    process.exit(1);
  }
}

bootstrap();

What I’ve checked so far:

  • The auth-service starts successfully with no errors.
  • The api-gateway runs fine.
  • Both services are running on the same host (127.0.0.1).
  • Port 3001 is free before running the services.

Still, whenever I call a route from the api-gateway that depends on the AUTH_SERVICE, I get a 500 Internal Server Error.

4
  • 500 is a deliberately vague message. Go to the server error log before posting on Stack Overflow and find the actual cause of the 500.message. once you have that you can start diagnosing the problem. Commented Oct 27 at 19:43
  • but this is the response im getting from nest my friend, i know it is vague Commented Oct 27 at 20:38
  • Is there any error in the auth-service server? I would bet that there's an error higher up, and as the error comes back to your gateway it's being reported as a generic HTTP 500 because it's an unhandled microservice error Commented Oct 27 at 23:30
  • I have doubts about the approach. I also find it surprising to see the port several times. Shouldn't there be @MessagePattern somewhere? The auth service doesn't seem to be handling incoming requests. Otherwise, add logs to see what's going on everywhere. Commented Oct 28 at 6:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.