Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Creating a project

A NestJS project is created by the Nest CLI command-line tool. It prepares the folders, TypeScript settings, tests and a first working route, so the server runs a minute after installation.

Installing the CLI and a new project

npm i -g @nestjs/cli
nest new blog
cd blog
npm run start:dev

nest new asks which package manager to use — choose npm. npm run start:dev starts the server in watch mode: it restarts after every file save. Open http://localhost:3000 — you will see the text Hello World!.

What is inside

blog/
├── src/
│   ├── app.controller.ts
│   ├── app.controller.spec.ts
│   ├── app.module.ts
│   ├── app.service.ts
│   └── main.ts
├── test/
├── nest-cli.json
├── package.json
└── tsconfig.json
FilePurpose
src/main.tsEntry point: creates the application and starts the server
src/app.module.tsRoot module, every other module is connected to it
src/app.controller.tsSample controller with the GET / route
src/app.service.tsSample service that returns Hello World!
*.spec.tsUnit tests
test/End-to-end (e2e) tests

The entry point

// src/main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
 
async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(process.env.PORT ?? 3000);
}
bootstrap();

NestFactory.create() builds the application from the root module, and listen() starts the server on the port from the PORT environment variable or on 3000. This file rarely changes: only global settings are added here.

Decorators

Open src/app.module.ts:

// src/app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
 
@Module({
  imports: [],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

@Module(...) is a decorator. It is a function called with @ in front of a class, method, property or parameter, and it attaches extra information to it. The AppModule class itself is empty; the decorator tells NestJS: "this is a module, here are its controllers and services". Decorators are a TypeScript feature; plain JavaScript does not have them yet.

The framework reads this information at startup and builds the application from it. That is why almost all NestJS code consists of classes with decorators.

Scripts

CommandWhat it does
npm run start:devRun and restart on changes
npm run buildCompile TypeScript into the dist/ folder
npm run start:prodRun the compiled code from dist/
npm run testUnit tests

Common mistake

If you start the server with npm run start, it will not see your changes: that script compiles the code once. During development always use npm run start:dev. And if port 3000 is already taken by another process, the server crashes with EADDRINUSE — stop the old process or set another port in the PORT variable.

Thanks for reading!

Donate to the site — we work hard for you.