Building Your First NestJS Application: A Step-by-Step Guide

NestJS is a popular Node.js framework for building server-side applications. It’s built on top of TypeScript, which enables predictable and readable code due to its strong typing and other advanced language features. Inspired by Angular, NestJS applies many of its design patterns and concepts to the back end. You can use it with any JavaScript […]

by Mirela Tsvetkova

May 9, 2023

4 min read

NodeJS software development scaled 1 - Building Your First NestJS Application: A Step-by-Step Guide

NestJS is a popular Node.js framework for building server-side applications. It’s built on top of TypeScript, which enables predictable and readable code due to its strong typing and other advanced language features. Inspired by Angular, NestJS applies many of its design patterns and concepts to the back end. You can use it with any JavaScript client of your choice, including React and Vue.

In this article, I’m going to introduce you to NestJS and give you a step-by-step guide to building your first NestJS application.

When to use NestJS?

NestJS is an excellent choice for building a back-end system for businesses that need to manage large amounts of sensitive data, transactions, and security protocols – as well as handle a massive volume of requests and support millions of users’ interactions.

For example, e-commerce platforms, banking and financial institutions, streaming services, gaming companies, transportation and logistics companies, and social media platforms are among the many businesses that can benefit from NestJS’s robust features and modular architecture.

NestJS Benefits

NestJS is a top-tier framework if you’re looking to streamline your development process and maximize productivity. Its modular architecture and powerful features enable you to create reusable and maintainable code, increasing efficiency and cutting down on development time. Ultimately,  NestJS lets you focus on higher-level tasks while building complex applications quickly and efficiently. One of NestJS’s standout features is its built-in security measures. By offering authentication, validation, and encryption out of the box, you can make sure your applications are secure without investing extra time or resources. Moreover, NestJS’s built-in testing utilities make it easy to write and execute tests, improving the overall quality and reliability of code.

Step-by-step guide about how to create a basic NestJS project:

Now that we’ve covered what NestJS is and its main benefits and use cases, it’s time to talk about creating your first NestJS project.

1. Navigate to the directory where you want to create your project

If you’re already familiar with using npx, you can simplify the process of installing and creating a new Nest project by typing the following command in your terminal

npx @nestjs/cli new basic-project

By using npx, you can bypass the second and third steps and proceed directly to step four.

2. Open your terminal

Once it’s open, type:

npm install -g @nestjs/cli

With this command, you’re installing a NestJS command-line interface tool that helps you to initialize your application.

3. To create your first NestJs project, type in:

nest new basic-project

image 2023 05 10T13 44 35 178Z - Building Your First NestJS Application: A Step-by-Step Guide

3.1 Pick a name for your project

nest new <your_project_name>

3.2 Select a package manager.

For this project, we will be using npm

3.3 src folder

After scaffolding the project, you will find a basic-project folder in your IDE. Once you open this folder, you will see a src folder

image 2023 05 10T13 45 27 534Z - Building Your First NestJS Application: A Step-by-Step Guide

4. Đžur first stop is the main.ts file

import { 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 starting point of our application, where Nest will begin building the dependency graph. Using NestFactory, we can create an instance of our application by providing it with the root module. The NestFactory create method returns a promise that, when resolved, provides a reference to the NestApplication instance. This reference can be used to manage the application’s lifestyle, including starting and stopping the server, registering middleware, and handling requests. The main.ts file contains the configuration for the server’s listening port — 3000.

5. app.module.ts

A controller and a module are the only essential components required to create a simple Nest application. But what exactly is a module?

import { Module } from ‘@nestjs/common’;

import { AppController } from ‘./app.controller’;

@Module({

 controllers: [AppController],

})

export class AppModule {}

In NestJS, modules serve to organize an application into distinct scopes. The metadata that comes from the @Module decorator helps Nest to build the application structure. The classes that are declared in the module scope form the application graph. In the module we can define controllers and providers, and we can import and export providers. In the simple example, the app has one single root module placed in app.module.ts and defines AppController. To simplify, we’ll remove the service from providers.

6. app.controller.ts

In order to simplify our example, we will remove the service and change the getHello function to init within the controller. This will result in the following controller definition.

import { Controller, Get } from ‘@nestjs/common’;

@Controller()

export class AppController {

 @Get()

 init(): string {

   return ‘Our amazing journey with Nest starts now!’;

 }

}

Controllers in Nest are responsible for handling incoming requests from clients, processing them using business logic, and sending back responses to the client. A controller is a class that serves as a route handler, providing the context for one or more related routes. This way, developers can easily define and organize the various routes and associated handlers within their Nest application.

7. Start your application

Congratulations! To launch your new NestJS application, navigate to the project folder using the terminal with the cd command and then run the command npm run start:dev

cd basic-project

npm run start:dev

Navigate to https://localhost:3000/

Nest-browser-localhost

Categories

Middle Software Engineer 1 at Dreamix