Single repository, multiple NestJS projects

Joel Garcia Jr
3 min readFeb 24, 2022

I know that are a lot of frameworks which promise to solve this with the “monorepo” concept, but I want to demonstrate how to achieve this without be locked-in in a framework or automation. It’s is possible to reach it with a good decoupled code and Docker.

I created and published a repo to demonstrate my vision about a single repository with multiple projects.

Follow the flow:

Installation

Git clone this repo

git clone https://github.com/joelgarciajr84/nestjs-monorepo-microservices-proxyUse docker compose to select which package do you wanna run.

Usage

# To run only the Marvel API run: docker-compose up marvel_api # To run DC API and Auth api run docker-compose up auth_api marvel_api

What about the other packages(APIs)?

The project contains a cockpit project that works as a reverse proxy NGINX for your APIs, for instance, imagine you are working on a feature that only affects marvel_api and dc_api you don’t have to run auth_api locally, so, how to run the tests and debug

Simple, with NGINX fallback:

With the settings above when was necessary to interact with auth_api the reverse proxy will redirect for the service running on homolog server, so the only thing you need to do is to interact is with your work feature.

Since NGINX is listening on port 8080, locally you can set Postman base url to:

http://localhost:8080/your_svc_name

Where is the shared content?

IMHO a microservice could not import a package from another, the best way to share common code between services is via packages, in this case, npm libraries published with scope and in a private repo, such as Azure Artifacts.

This approach makes the container much lightweight, easy to test and to deploy.

How to split the deploy?

In our approach we are using Github workflows, very easy to use, see:

What the code above does is, when whe push changes to the main branch at the path src/packages/auth (auth_api) the pipe will run, simple like that! :)

What about the Debug process?

You can use .vscode/launch.json to attach to the container:

Originally published at https://dev.to on February 24, 2022.

--

--