Table of Contents
- Prerequisites
- Installing NestJS CLI
- Creating a New Project
- Understanding the Project Structure
- Running Your First NestJS Server
- Exploring Core Files:
main.ts
,AppModule
,AppController
, andAppService
- Summary and Next Steps
1. Prerequisites
Before we start, make sure you have the following installed:
- Node.js (LTS version recommended): https://nodejs.org
- npm or yarn
- Basic understanding of TypeScript
- Git (optional but useful)
2. Installing NestJS CLI
NestJS provides a powerful CLI to scaffold and manage projects efficiently.
bashCopyEditnpm i -g @nestjs/cli
To confirm installation:
bashCopyEditnest --version
This CLI helps you:
- Generate modules, controllers, and services
- Scaffold a new project
- Run and build applications
- Test and lint your code
3. Creating a New Project
To create a new NestJS project:
bashCopyEditnest new my-nest-app
You’ll be prompted to choose a package manager (npm/yarn/pnpm). Pick your preference.
Once done, navigate into the project:
bashCopyEditcd my-nest-app
4. Understanding the Project Structure
Here’s the default project structure:
pgsqlCopyEditmy-nest-app/
├── src/
│ ├── app.controller.ts
│ ├── app.controller.spec.ts
│ ├── app.module.ts
│ ├── app.service.ts
│ └── main.ts
├── test/
│ └── app.e2e-spec.ts
├── .eslintrc.js
├── .prettierrc
├── nest-cli.json
├── package.json
├── tsconfig.json
└── README.md
Key Files:
main.ts
– Entry point of the app, bootstraps Nest.app.module.ts
– Root module of the app.app.controller.ts
– Handles HTTP requests.app.service.ts
– Business logic for the controller.
Nest uses modules to group related components, promoting modularity and separation of concerns.
5. Running Your First NestJS Server
To start the development server:
bashCopyEditnpm run start:dev
This runs your app in development mode with hot-reloading via ts-node-dev
.
Now open http://localhost:3000, and you should see:
bashCopyEditHello World!
This message is returned by the default AppController
.
6. Exploring Core Files
main.ts
tsCopyEditimport { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();
This is the entry point of your app. It uses the Nest Factory to create and run an instance of your application.
app.module.ts
tsCopyEditimport { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
This defines the application’s root module using the @Module()
decorator.
app.controller.ts
tsCopyEditimport { 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();
}
}
It defines a route (/
) and maps it to the getHello()
method from the AppService
.
app.service.ts
tsCopyEditimport { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
}
}
Provides a reusable service, injected into the controller.
7. Summary and Next Steps
We’ve successfully set up a NestJS project using the CLI, explored the project structure, and understood how a simple app runs. This foundational understanding is key to mastering the framework.