What is NestJS
NestJS in One Line
NestJS is a framework for building server-side applications (REST APIs, GraphQL APIs, WebSocket servers, microservices) with Node.js and TypeScript.
It runs on top of an HTTP library. By default that library is Express; you can switch to Fastify with one line. NestJS adds what Express does not give you: a clear project structure, dependency injection, validation, guards, testing tools and much more.
- Written in TypeScript and built for TypeScript (decorators like
@Controller()and@Get()). - Opinionated structure: modules, controllers and providers. Every NestJS project looks similar, so a new team member is productive quickly.
- Batteries included: validation, configuration, authentication, database integration, testing, OpenAPI docs, queues, WebSockets, microservices.
- Open source (MIT licence), created by Kamil Myśliwiec in 2017. Current major versions are 10 and 11.
A First Look
This is a complete NestJS controller. Even before learning the details you can guess what it does:
src/hello/hello.controller.ts
import { Controller, Get } from '@nestjs/common';
@Controller('hello') // all routes start with /hello
export class HelloController {
@Get() // GET /hello
sayHello() {
return { message: 'Hello from ShopEase API' }; // returned object becomes JSON
}
}Example
Call it with curl:
curl -i http://localhost:3000/helloExample output
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 39
{"message":"Hello from ShopEase API"}What You Will Build
Through this material you build the backend of ShopEase, an e-commerce app with products, categories, users, carts and orders. The same project is used in the TypeScript and Next.js materials, where the Next.js material builds the storefront that calls this API.
Note: You should know basic JavaScript and TypeScript (types, classes, interfaces, async/await) before starting. See the TypeScript material if classes or decorators are new to you.