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"
}
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.
