Leadership

Getting Started with NestJS: A Framework for Opinionated Backend Development

I like NestJS because it makes decisions for you. In a world where Express gives you a blank canvas and says "good luck," NestJS gives you an architecture...

29 Dec 2023

Getting Started with NestJS: A Framework for Opinionated Backend Development

I like NestJS because it makes decisions for you. In a world where Express gives you a blank canvas and says "good luck," NestJS gives you an architecture. Controllers, services, modules -- all structured from the start.

It builds on top of Express (or Fastify) and borrows heavily from Angular's architecture. If you have worked with Angular, NestJS will feel familiar. If you have not, the learning curve is worth it for the structure it provides.

Getting Started

Install the CLI and scaffold a project:

Text
npm i -g @nestjs/cli
nest new project-name

The CLI generates a project with an MVC structure out of the box. The entry point is main.ts. The controller handles routes. The service handles business logic.

Start the dev server:

Text
npm run start:dev

Building an Endpoint

Say you need a JSON endpoint for a "cats" resource. One command:

Text
nest g controller cats

This generates cats.controller.ts. Create a service to handle the logic:

Text
import { Injectable, Res } from '@nestjs/common'

The separation between controllers and services is enforced by the framework. That is the opinionated part -- and it is a feature, not a limitation.

Why It Matters

On teams, opinions reduce arguments. When the framework dictates project structure, you spend less time debating folder layouts and more time shipping features. New developers onboard faster because every NestJS project looks the same.

The trade-off is flexibility. If you need something truly custom, NestJS constraints can feel restrictive. For most CRUD-heavy backend services, though, the structure pays for itself quickly.