Microservices architecture has revolutionized the way developers build scalable and maintainable applications. NestJS, a progressive Node.js framework, makes it straightforward to develop modular microservices. This tutorial guides you through building a modular microservices system using NestJS, step by step.

Prerequisites

  • Basic knowledge of Node.js and JavaScript/TypeScript
  • Node.js and npm installed on your machine
  • Understanding of microservices architecture concepts
  • NestJS CLI installed globally (`npm i -g @nestjs/cli`)

Setting Up the Project

Create a new NestJS project using the CLI:

nest new microservices-project

Navigate into the project directory:

cd microservices-project

Creating Modular Microservices

In NestJS, each microservice can be a separate module. Let's create two microservices: User and Order.

Generate modules using CLI:

nest generate module users

nest generate module orders

Building the User Microservice

In src/users/users.module.ts, define the user module:

import { Module } from '@nestjs/common';

@Module({

  controllers: [],

  providers: [],

})

export class UsersModule {}

Creating User Service

Generate a service:

nest generate service users

Implement the service logic in src/users/users.service.ts:

import { Injectable } from '@nestjs/common';

@Injectable()

export class UsersService {

  private users = [{ id: 1, name: 'Alice' }];

  findAll() {

    return this.users;

  }

Building the Order Microservice

Similarly, define the order module in src/orders/orders.module.ts:

import { Module } from '@nestjs/common';

@Module({

  controllers: [],

  providers: [],

})

export class OrdersModule {}

Creating Order Service

Generate the order service:

nest generate service orders

Implement the order logic in src/orders/orders.service.ts:

import { Injectable } from '@nestjs/common';

@Injectable()

export class OrdersService {

  private orders = [{ id: 1, item: 'Book' }];

  findAll() {

    return this.orders;

Connecting Microservices

To enable communication between microservices, NestJS supports various transport layers like TCP, Redis, or MQTT. For simplicity, we'll set up TCP microservices.

In the main application file src/main.ts, create a microservice for each module:

import { NestFactory } from '@nestjs/core';

import { AppModule } from './app.module';

async function bootstrap() {

  const app = await NestFactory.create(AppModule);

  app.connectMicroservice({

    transport: Transport.TCP,

  });

  await app.startAllMicroservices();

  await app.listen(3000);

} bootstrap();

Testing the Microservices

Start each microservice in separate terminals:

npm run start

Use tools like Postman or curl to send requests to localhost:3000 and verify responses from each microservice.

Conclusion

Building modular microservices with NestJS enhances scalability and maintainability. By creating separate modules and services, and connecting them via transport layers, developers can design robust distributed systems. Experiment with different communication protocols and expand your microservices architecture to suit complex application needs.