NestJS Fundamentals
Overview
NestJS is a progressive Node.js framework built with TypeScript, inspired by Angular's architecture. This tutorial introduces the core concepts: modules, controllers, providers, and dependency injection. You will build a RESTful API with a modular structure that scales beautifully.
1. Introduction to NestJS
NestJS is built on top of Express (or Fastify) and uses TypeScript by default. Key features:
- Modular architecture: Organise code into modules.
- Dependency Injection (DI): Decouple components with inversion of control.
- Decorators: TypeScript decorators define controllers, routes, and more.
- Middleware & Guards: Request‑handling pipeline.
- Testing: Built‑in support for unit and e2e testing.
2. Setting Up a NestJS Project
Prerequisites
- Node.js (v16+)
- npm or yarn
Installation
The CLI generates a project with:
src/main.ts– application entry pointsrc/app.module.ts– root modulesrc/app.controller.ts– example controller
3. Controllers & Routing
Controllers handle incoming requests. They use decorators to define routes:
@Controller('path')– base route prefix@Get(),@Post(),@Put(),@Delete()@Param('id')– extract URL parameter@Body()– extract request body@Query('search')– extract query string
class-validator.
4. Providers & Dependency Injection
Providers are classes that can be injected into controllers or other providers.
They are registered in a module's providers array.
NestJS uses the IoC container to manage dependencies. The
@Injectable() decorator marks a class as injectable.
5. Modules & Application Structure
Modules organise your application. Each module encapsulates related controllers, providers, and imports.
- Root module:
AppModule– the entry point. - Feature modules:
UsersModule– group related features. - Shared modules: Export providers for reuse.
Quiz
Question 1
Which decorator is used to define a controller in NestJS?
- @Service
- @Controller
- @Component
- @Module
Show answer
Question 2
What is the purpose of the @Injectable() decorator?
- To define a controller
- To make a class available for dependency injection
- To define a module
- To create a route
Show answer
Question 3
How do you extract a URL parameter like id from
/users/42?
- @Param('id') id: string
- @Query('id') id: string
- @Body('id') id: string
- @Headers('id') id: string
Show answer
Exercises
Exercise 1
Create a ProductsController with GET /products and
GET /products/:id. Use a ProductsService to handle the data.
Sample answer
Exercise 2
Add a POST /products endpoint that accepts a body and returns
the created product.
Sample answer
Homework
Homework 1
Set up a NestJS project with two modules: UsersModule and
PostsModule. Each module should have a controller and a service. Users have
many posts (1:N).
Sample answer
Structure:
users/users.module.ts– importsPostsModuleusers/users.controller.ts–GET /users/:id/postsusers/users.service.ts– data methodsposts/posts.module.ts,posts/posts.controller.ts,posts/posts.service.ts
Use dependency injection to share PostsService across modules (export
the service from PostsModule and import it in
UsersModule).
Mini‑Project
Task Management API
Build a Task Management API with NestJS:
GET /tasks– list all tasksGET /tasks/:id– get single taskPOST /tasks– create a task (title, description)PUT /tasks/:id– update a taskDELETE /tasks/:id– delete a task- Use an in‑memory array as a data store
Sample implementation outline
TaskService:
tasks: Task[] = []findAll(),findOne(id: number),create(taskDto),update(id, taskDto),delete(id)
TaskController:
@Get() findAll()@Get(':id') findOne(@Param('id') id: string)@Post() create(@Body() dto)@Put(':id') update(@Param('id') id: string, @Body() dto)@Delete(':id') delete(@Param('id') id: string)
Tutorial Summary
You learned the fundamentals of NestJS: setting up a project, creating controllers with routing, building providers with dependency injection, and organising code into modules. You built a REST API with a clean, modular architecture that scales with your application.
Key takeaway: NestJS's decorator‑based approach and DI container make it easy to build maintainable, testable backend applications.