#HTTP API Gateway for Microservices

13 messages · Page 1 of 1 (latest)

north gate
#

Hello,
I have a question.
I would like to build an application using a microservices architecture with NestJS.

For example: I want to create one microservice for managing my users and another for handling ToDo lists (imagine a ToDo application for connected users—this is just an example, not the actual idea).

So, I create my two services in a monorepo setup, as shown in the documentation. Now, both of my services are working well, but I don’t know how to handle the Gateway.

Do you have any links, tutorials, or docs that could help me, please?

I've never worked with microservices before...

glossy pivot
#

Your gateway will be responsible for receiving HTTP requests such as GET POST etc., and will direct the payload to the microservices, there is no secret. So to simplify, your gateway will work "similar" to a normal monolithic.

north gate
glossy pivot
# north gate Ok. But how can we achieve that ?

https://docs.nestjs.com/microservices/basics

import {
  Body,
  Controller,
  Get,
  HttpCode,
  Logger,
  Param,
  Post,
} from "@nestjs/common";
import { Client, ClientProxy, Transport } from "@nestjs/microservices";
import { ApiTags } from "@nestjs/swagger";

import { BodyMathDto } from "./dto/body-math.dto";

@ApiTags("math")
@Controller("math")
export class MathController {
  private readonly logger = new Logger(MathController.name);

  @Client({
    transport: Transport.TCP,
    options: {
      host: "127.0.0.1",
      port: 8877,
    },
  })
  private readonly client: ClientProxy;

  @Get()
  getHello() {
    return this.client.send<string, string>("getHello", "");
  }

  @Post("add")
  @HttpCode(200)
  add(@Body() body: BodyMathDto) {
    return this.client.send<string, BodyMathDto>("add", body);
  }

  @Post("subtract")
  @HttpCode(200)
  subtract(@Body() body: BodyMathDto) {
    return this.client.send<string, BodyMathDto>("subtract", body);
  }

  @Post("multiply")
  @HttpCode(200)
  multiply(@Body() body: BodyMathDto) {
    return this.client.send<string, BodyMathDto>("multiply", body);
  }

  @Post("divide")
  @HttpCode(200)
  divide(@Body() body: BodyMathDto) {
    return this.client.send<string, BodyMathDto>("divide", body);
  }

  @Get(":id")
  testParam(@Param("id") id: string) {
    return this.client.send<string, string>("testParam", id);
  }
}
north gate
glossy pivot
# north gate And how can we trigger the different error like 400, 401, etc in the service ?
north gate
#

I can't understand how we trigger if it's an error 401, 400, or whatever ?

glossy pivot
#

I create a global filter in the gateway and in the microservices I throw something like this

throw new RpcException(new BadRequestException(errorMessages)); 

Then in my filter there in the gateway I respond with the correct information based on what the filter received.

north gate
#

Okk okkk

#

And if we have a huge app, with like 500 endpoints for example, do we need to have every controller twice ?
Like once for the HTTP app, and once again for the service ?

glossy pivot
#

Of course yes

#

You need to be patient and read a lot, you won't learn this kind of thing in 1 day, and I'm not going to give a free course in a discord reply to teach "Micro services" to someone. Over time you will understand what you have to do and how.