Released · improving
Framework · Since 2017
NestJS is a TypeScript framework for structured Node.js servers built on modules and dependency injection, running on Express, the minimal web framework.
Express is the most widely used web framework for Node.js. It provides a small core of routing and middleware and leaves application structure to you. NestJS, first released in 2017, is a server framework built on TypeScript and decorators that adds modules, controllers, providers and dependency injection. Nest delegates HTTP handling to a platform adapter, and the default is Express, so a Nest application runs as an Express server under the hood and can use Express middleware directly.
Express suits small APIs, prototypes and projects where you want to design the structure yourself. NestJS pays off as codebases and teams grow: long-lived REST and GraphQL APIs, microservices and real-time servers. Pipes validate input, guards enforce authorization, and the framework integrates cleanly with Jest-based testing and ORMs such as TypeORM and Prisma.
Once Node.js is installed, Express takes nothing more than npm install express and a few lines of code. For NestJS, create a project with the Nest CLI and read the official documentation starting with First steps. Getting comfortable with Express requests, responses and middleware first makes it easier to see what Nest adds on top.
Split features into modules and receive providers such as services through constructors, reducing coupling and making testing easier.
Declare routes and inputs with decorators like @Controller, @Get and @Body, and add cross-cutting logic with pipes, guards and interceptors.
Express is the default HTTP platform, so its middleware ecosystem works as is, and you can switch to Fastify when needed.
On its own, Express offers a small core of routing and middleware for building servers quickly in whatever shape you choose.
Both NestJS and Express run on Node.js. Install the LTS release from nodejs.org and confirm that node and npm print their versions.
node --version && npm --versionInstall the official command-line tool that creates projects and generates code. For Express alone, skip this and run npm install express in your project folder instead.
npm i -g @nestjs/cliCreate a new project and start the development server in watch mode. The CLI asks which package manager to use; when it finishes, the app responds at http://localhost:3000.
nest new hello-nest && cd hello-nest && npm run start:devA project created with nest new contains this controller. @Controller() registers the class as a controller, and @Get() maps GET requests on the root path to getHello. AppService is injected through the constructor, while src/main.ts creates the application from AppModule and listens on port 3000.
src/app.controller.ts
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
getHello(): string {
return this.appService.getHello();
}
}
npm run start:devSix chapters that take you from installation to the core ideas of NestJS · Express.
Ask questions, share experience and trade opinions about NestJS · Express.
No discussions yet. Start the first one.
0 comments
Sign in · Sign in to leave a comment.
Be the first to comment.