NashTech Insights

Getting Started with NESTJS

Deeksha
Deeksha
Table of Contents

Introduction

NestJS is a popular Node.js framework that provides a powerful set of tools for building scalable, modular, and maintainable server-side applications. It is built on top of the Express.js framework and provides a higher-level abstraction that makes it easier to develop complex applications. With its modular architecture, dependency injection, and extensive ecosystem, NestJS provides developers with a solid foundation for building enterprise-grade applications. In this blog post, we will explore the key concepts and steps to get started with NestJS and unlock its full potential.

Prerequisites

Before begin, ensure that you have the following prerequisites set up on your development machine:

  • Node.js (version 14 or higher)
  • npm (version 6 or higher)
  • TypeScript: NestJS is built on TypeScript, so make sure you have it installed globally.
  • IDE or code editor of your choice (Visual Studio Code recommended)

Create a new NestJS application

To create a new NestJS project, you can use the Nest CLI, which simplifies project setup and provides useful generators. Open your terminal and run the following command to install the Nest CLI globally:

npm install -g @nestjs/cli

Once installed, you can create a new NestJS project by running:

nest new project-name

Replace “project-name” with the desired name for your project. The CLI will set up a new project structure with some initial files.

Project Structure

NestJS follows a modular architecture, which promotes separation of concerns and code reusability. Here’s an overview of the project structure generated by the Nest CLI:

  • src: The source directory where your application’s code resides.
    • app.module.ts: The root module that orchestrates the application’s components.
    • app.controller.ts: An example controller that handles incoming requests.
    • app.service.ts: An example service that contains business logic.
  • Entry point: main.ts is the entry point of the NestJS application. It uses NestFactory.create and uses AppModule to create the app.
  • test: The directory for writing tests.
  • node_modules: The directory where your project’s dependencies are installed.

Building Blocks

NestJS applications are built using three primary building blocks: modules, controllers, and services.

NestJs Modules

Modules are used to organize your application into distinct, reusable parts. To create a new module, run the following command:

nest g module users

This will create a new module named “users” in your application. You can then add controllers, services, and other components to this module.

NestJs Controllers

Controllers are responsible for handling incoming requests and returning responses to the client. To create a new controller, run the following command:

nest g controller users

This will create a new controller named “users” in your application. You can then define routes and handlers for this controller.

NestJs Services

Services are responsible for handling business logic and interacting with data sources such as databases and APIs. To create a new service, run the following command:

nest g service users

This will create a new service named “users” in your application. You can then implement methods to handle business logic and data interactions.

Dependency Injection

NestJS leverages dependency injection to manage the creation and resolution of objects. It allows for better modularity, testability, and maintainability. You can inject services or other dependencies into your controllers and modules using decorators provided by NestJS. To use dependency injection, you can simply add the “@Injectable()” decorator to your service class, and then inject it into your controller using the constructor:

@Injectable()
export class UsersService {
// …
}
@Controller()
export class UsersController {
constructor(private readonly usersService: UsersService) {}
// …
}

Run the application

Once the application is created, navigate to the application folder using the following command:

cd project-name

Next, run the application using the following command:

npm run start

This will compile your TypeScript code into JavaScript and start the application using the Node.js runtime. By default, your application will run on http://localhost:3000/.

Building APIs

NestJS provides decorators to define routes and handle HTTP requests. You can use decorators like @Get(), @Post(), @Put(), and @Delete() to create RESTful endpoints. These decorators can be applied to controller methods, which will be executed when a matching request is received.

Conclusion

NestJS is an excellent choice for building scalable and maintainable server-side applications with Node.js. Its modular architecture, dependency injection, and extensive ecosystem make it a powerful framework for enterprise-grade projects. By following the steps outlined in this blog post, you can quickly get started with NestJS.

For more such posts, please follow our LinkedIn page- FrontEnd Competency.

Deeksha

Deeksha

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

%d bloggers like this: