NestJS is a progressive Node.js framework that simplifies the creation of server-side applications. Whether you're building REST APIs, microservices, or GraphQL applications, NestJS offers a structured and modern approach to development. This article will guide you through the installation and initial setup of a NestJS project.
Prerequisites
- NodeJS and NPM
- Nest CLI
Steps to Install and Setup NestJS application
Step 1: Install the Nest CLI
The Nest CLI provides an efficient way to create and manage NestJS projects. To install the CLI globally, run the following command in your terminal:
npm install -g @nestjs/cliYou can verify the installation by checking the CLI version:
nest --versionStep 2: Create a New Project
To create a new NestJS project, use the Nest CLI:
nest new nest-gfgThe CLI will prompt you to choose a package manager (npm or yarn) for installing dependencies. After making your choice, the CLI sets up the project structure and installs the necessary packages.
Navigate into your project directory:
cd project-nameFolder Structure
Dependencies
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1"
}
Key Files and Directories
- src: Contains the source code of your application.
- app.controller.ts: Defines a basic controller.
- app.module.ts: The root module of your application.
- app.service.ts: A basic service.
- main.ts: The entry point of your application.
- test: Contains test files.
- package.json: Lists the dependencies and scripts for your project.
- tsconfig.json: TypeScript configuration file.
- nest-cli.json: Nest CLI configuration file.
Example: Here is a basic NestJS application
//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();
}
}
//app.service.ts
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getHello(): string {
return 'Hello, NestJS!';
}
}
//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 { }
//main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();
Step 3: Run the Application
To start the application, use the following command:
npm run startBy default, the application runs on 'http://localhost:3000'.
You can also run the application in development mode with live reloading:
npm run start:devThis mode watches for file changes and automatically restarts the server.
Output
Additional Tips and Best Practices
1. Use TypeScript Features: Use TypeScript’s static typing and interfaces for more robust code.
2. Organize Your Code: Keep your code modular by creating separate modules for different features or domains.
3. Use DTOs: Use Data Transfer Objects (DTOs) to validate and transform incoming request data.
4. Enable CORS: Enable Cross-Origin Resource Sharing (CORS) if your application needs to interact with clients from different origins.
5. Security: Use guards and interceptors to handle authentication and authorization.