Fullstack CourseLearn by building
Back to week 1

Topic

Nest architecture & CLI

Definition

NestJS is a TypeScript-first Node.js framework that layers an opinionated application architecture — modules, controllers, and providers — on top of an underlying HTTP platform such as Express, and ships a CLI that scaffolds and builds projects following that architecture.

In simpler words

Nest is Express (or Fastify) with a structure enforced on top, plus a command-line tool that generates the files and builds the project for you.

After Node and Express, map what Nest adds: modules, DI, decorators, and the CLI that scaffolds them. Nest ≠ Next — Nest is this monorepo’s HTTP API and database owner; the React/Next frontend is a client that calls it over HTTP.

After this you can

  • Explain what Nest adds on top of Express
  • Read nest-cli.json and know what nest build/start do
  • Trace apps/api/src/main.ts from bootstrap to listening port
  • Say why Nest and Next are unrelated despite similar names

Nest vs Next — and what Nest actually is

Definition

NestJS is a backend Node.js framework for building HTTP APIs, while Next.js is a frontend/full-stack React framework for building rendered UI; the two share a similar name but no codebase or runtime relationship.

In simpler words

Nest answers HTTP requests with JSON and owns the database; Next renders React pages. This monorepo uses both, for different jobs.

apps/api is a Nest application: it owns Postgres (via TypeORM), the tickets/health/auth HTTP surface, and returns JSON.

apps/web is a Next.js application: it renders React UI and calls apps/api over HTTP/cookies. It does not import Nest modules or touch the database directly.

Nest sits on an HTTP platform adapter — Express by default (Fastify is swappable) — and adds modules, controllers, providers, and a dependency-injection container around it.

Two apps, two jobs

apps/api   → NestJS  → owns HTTP routes + Postgres (TypeORM)
apps/web   → Next.js → renders React, calls apps/api over fetch/axios + cookies

If you find yourself writing SQL in apps/web or JSX in apps/api, you are in the wrong app.

What the Nest CLI generates and builds

Definition

The Nest CLI (@nestjs/cli) is a command-line tool that scaffolds new Nest projects and files from schematics, and compiles a project according to nest-cli.json.

In simpler words

nest new starts a project, nest generate adds a piece (module/controller/service) with the right boilerplate, and nest build/start compile and run it.

apps/api/nest-cli.json sets sourceRoot: "src" and deleteOutDir: true so nest build cleans dist/ before compiling.

apps/api/package.json wires the CLI: "build": "nest build" and "dev": "nest start --watch". There is no nest generate script wired here — the CLI is a devDependency you can still run directly (npx nest generate module <name>) to scaffold new features consistently.

apps/api build/dev scripts

{
  "scripts": {
    "build": "nest build",
    "dev": "nest start --watch",
    "start": "node dist/main.js"
  }
}

nest start --watch recompiles and restarts on save during local development.

Mistake: hand-rolling module boilerplate

// Wrong — copy-pasted from another feature, drifts from convention
touch tickets.module.ts // then guess the shape

// Right
npx nest generate module tickets
npx nest generate controller tickets
npx nest generate service tickets

Generators keep @Module/@Controller/@Injectable wiring consistent across a team.

From CLI output to a running app

Definition

A compiled Nest application boots by creating a root application instance from the root module, then Node executes the resulting HTTP listener.

In simpler words

nest build turns TypeScript into dist/, then node dist/main.js starts the server that main.ts describes.

apps/api/src/main.ts calls NestFactory.create(AppModule) to build the app from the root module, applies cookies/CORS/global pipes/filters/interceptors, sets up Swagger at /docs, then calls app.listen(env.PORT).

AppModule is the entry point the CLI-built app boots from — every controller and provider must be reachable from it through module imports.

apps/api bootstrap shape

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.use(cookieParser());
  app.enableCors({ origin: env.CORS_ORIGIN, credentials: true });
  app.useGlobalPipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true }));
  app.useGlobalFilters(new AllExceptionsFilter());
  await app.listen(env.PORT);
}

Everything global (pipes, filters, CORS) is configured once here, before the app starts listening.

Keep in mind

  • Nest CLI scaffolding matters less than the architecture it encodes — modules, controllers, providers.
  • When exploring a new Nest app, start at main.ts, then follow AppModule’s imports.
  • Nest and Next never import each other in this monorepo — the HTTP boundary is the only connection.

Test

Check your understanding

At least 10 questions — mix of concept, syntax, practical, and logic. Score ≥ 80% (enforced by the API) to save progress.

Checking your session…

10 questions · concept 3 · syntax 2 · practical 3 · logic 2

Concept1. Which statement best defines Nest architecture and CLI?
Syntax2. Which code-level choice matches Nest architecture and CLI?
Practical3. A reviewer spots a bug related to Nest architecture and CLI. What is the right fix?
Logic4. Which reasoning correctly explains Nest architecture and CLI?
Concept5. Which boundary does correct use of Nest architecture and CLI preserve?
Practical6. What is the safest next step when applying Nest architecture and CLI?
Syntax7. Which implementation direction matches the rule for Nest architecture and CLI?
Logic8. Which consequence follows from applying Nest architecture and CLI correctly?
Concept9. Which claim about Nest architecture and CLI is true in this Nest + Postgres monorepo?
Practical10. Which team practice best demonstrates Nest architecture and CLI?