We'll be using NestJs, if you havent used it already its pretty much like Angular and I think it's a clever way to enable frontend developers to do thinks on the backend also. Anyway, it comes out with a CLI tool that allows generation of code. If you dont know whats a CLI this link explains it very well, same with NestJs, this link goes into detail of what NestJs is.
Assuming you know NestJs or that you read the articles I just gave you, let's go ahead and start coding.
In any microservices architecture you'll find multiple services running, either in the same machine or in totally distributed places. To start our small proof of concept, we'll create a service using the NestJs CLI. Just follow the next steps:
- Create a new folder and go to it using you preferred command line tool.
- Execute
nest new service-a
, it will prompt you to choose between npm and yarn, I used npm. - Delete the files
src/app.controller.spec.ts
andsrc/app.service.ts
. - Remove the
AppService
from the imports and theAppModule
provider. - Remove the
AppService
from the imports and theAppController
constructor.
The AppModule
will endpoint looking like this:
// src/app.module.ts
import { Module } from "@nestjs/common";
import { AppController } from "./app.controller";
@Module({
imports: [],
controllers: [AppController],
providers: []
})
export class AppModule {}
The AppController
will endpoint looking like this:
import { Controller, Get } from "@nestjs/common";
@Controller()
export class AppController {
@Get()
getHello(): string {
return "hello";
}
}
You got yourself your first service. Now is time to transform it into a microservice, thankfully NestJs covers a lot of it for you.