NestJS is a progressive Node.js framework for building efficient, scalable, and maintainable server-side applications. It leverages TypeScript by default (while still supporting JavaScript), and is heavily inspired by Angular’s architecture, incorporating features like dependency injection, decorators, and modular design. Built on top of Express (with optional support for Fastify), Nest provides a structured and opinionated way to develop robust backend applications, making it ideal for enterprise-level projects, RESTful APIs, microservices, and GraphQL-based systems. Its powerful CLI, rich ecosystem, and seamless integration with tools like TypeORM, Prisma, and Swagger make it a popular choice for modern backend development.
Running nest
To run nest use the command: npm run start:dev
Understanding Nest
For each Nest folder, it containst three files, a service, a controller and a module. Understanding these three parts are key to capitalising Nest's modular design.
Service:
A Service is a class that contains your business logic, database interactions, and other key logic. You can treat the service class as a regular python class that is in charge of creating all the necessary methods to perform data operations. When using Prisma, all service methods must be asynchronous. Services are where you:
Fetch data from a database
Perform complex calculations
Connect to third-party APIs
Manage your core application logic
An example service class for an employee would look like this.
employee.service.ts
import{Injectable}from'@nestjs/common';import{Prisma}from'@prisma/client';import{DatabaseService}from'src/database/database.service';@Injectable()exportclassEmployeeService{constructor(privatereadonlydatabaseService:DatabaseService){}asynccreate(createEmployeeDto:Prisma.EmployeeCreateInput){returnthis.databaseService.employee.create({ data: createEmployeeDto });}asyncfindAll(role?:'INTERN'|'ENGINEER'|'ADMIN'){if (role) {returnthis.databaseService.employee.findMany({ where:{ role },});}returnthis.databaseService.employee.findMany();}asyncfindOne(id:number){returnthis.databaseService.employee.findUnique({ where:{ id },});}asyncupdate(id:number,updateEmployeeDto:Prisma.EmployeeUpdateInput){returnthis.databaseService.employee.update({ where:{ id }, data: updateEmployeeDto,});}asyncremove(id:number){returnthis.databaseService.employee.delete({ where:{ id },});}}
Controller
A Controller defines the HTTP endpoints for your app. It’s responsible for handling requests (like GET, POST, PUT, DELETE) and returning responses. Each method in the controller corresponds to a route in your API, and will have a decorator (e.g. @GET()) followed by the actual method (in the next line). Controllers handle HTTP requests, but all logic is offloaded to services to keep the controller clean and focused.