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 an HTTP API and database owner, while a React/Next frontend is a client that calls it over HTTP.

After this you can

  • Explain what Nest adds on top of Express
  • Explain nest-cli.json and know what nest build/start do
  • Trace 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. A full-stack project can use both, for different jobs.

A Nest application owns the database (for example Postgres via TypeORM), exposes an HTTP surface such as tickets/health/auth routes, and returns JSON.

A Next.js application renders React UI and calls the Nest 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 the Next.js app or JSX in the Nest app, 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.

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

A package.json typically wires the CLI: "build": "nest build" and "dev": "nest start --watch". Even without a nest generate script wired in, the CLI is a devDependency you can run directly (npx nest generate module <name>) to scaffold new features consistently.

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.

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.

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 — 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…

15 questions · concept 5 · syntax 3 · practical 4 · logic 3

1. What is NestJS?
Concept
2. What is Nest unrelated to in this monorepo?
Syntax
3. What does the Nest CLI help with?
Practical
4. Which architectural unit groups features in Nest?
Logic
5. Where does HTTP handling start in Nest?
Concept
6. What supplies business logic dependencies?
Practical
7. Which bootstrap file typically creates the app?
Syntax
8. Why prefer Nest modules over one giant file?
Logic
9. Which statement about platforms is true?
Concept
10. What should nest generate create for a feature?
Practical
11. What HTTP route does this controller expose?
Conceptintermediate
@Controller('tickets')
export class TicketsController {
  @Get(':id')
  findOne(@Param('id') id: string) {}
}
12. Nest reports it cannot resolve the dependencies of TicketsController. What is the likely cause?
Syntaxadvanced
@Module({
  controllers: [TicketsController],
  providers: [],
})
export class TicketsModule {}
13. What is the role of this file?
Practicalintermediate
async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}
bootstrap();
14. What does AppModule do here?
Logicintermediate
@Module({ imports: [TicketsModule, AuthModule] })
export class AppModule {}
15. Which statement is correct about apps/api and apps/web?
Conceptadvanced
// apps/api (Nest) and apps/web (Next) in one monorepo

Checking your session…