Fullstack NestJS

Monorepos and Tooling


The Goal

We will discuss how available tooling such as Nx can greatly accelerate our ability to generate applications.

We will also talk about how we can decouple our applications while operating in the same code base using monorepos.

A few variations for how to structure your files will be explored with the pros and cons of each being pointed out.

As a bonus, we will see how you can create a few custom, lightweight tools to 10x your ability to create applications.

Our goal is to use Nx to scaffolds out a project based on our domain model.

The Commands

Add the Nest functionality to the Nx workspace.

npx nx add @nx/nest

Generate an API application for each feature.

npx nx g @nx/nest:app --name=challenges-api --directory=remote/challenges --projectNameAndRootFormat=as-provided
npx nx g @nx/nest:app --name=flashcards-api --directory=remote/flashcards --projectNameAndRootFormat=as-provided
npx nx g @nx/nest:app --name=notes-api --directory=remote/notes --projectNameAndRootFormat=as-provided
npx nx g @nx/nest:app --name=users-api --directory=remote/users --projectNameAndRootFormat=as-provided
npx nx g @nx/nest:app --name=features-api --directory=remote/features --projectNameAndRootFormat=as-provided

The Code

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

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

import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';

const configureSwagger = (app) => {
  const options = new DocumentBuilder()
    .setTitle('Users Api')
    .setDescription('API for Users')
    .setVersion('1.0')
    .build();
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('api', app, document);
};

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const globalPrefix = 'api';
  const port = process.env.PORT || 3400;

  app.enableCors();
  app.setGlobalPrefix(globalPrefix);

  configureSwagger(app);

  await app.listen(port);
  Logger.log(
    `🚀 Application is running on: http://localhost:${port}/${globalPrefix}`,
  );
}

bootstrap();
< Nest Home