Getting Started with NestJS: A Step-by-Step Example Guide

Johnstyle
3 min readJun 17, 2023

--

Introduction

NestJS is a powerful framework for building server-side applications with Node.js. If you’re new to NestJS and eager to get started, this step-by-step guide will walk you through the process with a practical example. By following along, you’ll gain a solid understanding of NestJS concepts and learn how to build a basic application from scratch.

Prerequisites

Before we dive into NestJS, make sure you have Node.js and npm (Node Package Manager) installed on your machine. You can download them from the official Node.js website (https://nodejs.org). Once you have Node.js and npm set up, you’re ready to begin.

Step 1

Project Setup Start by creating a new directory for your NestJS project. Open a terminal and navigate to the desired location. Run the following command to set up a new NestJS project using the Nest CLI:

$ npx @nestjs/cli new nest-example

This command will generate a new NestJS project structure in the “nest-example” directory. Once the command completes, navigate into the project directory:

$ cd nest-example

Step 2

Creating a Controller Controllers handle incoming requests and define the routes for your application. Generate a new controller using the Nest CLI:

$ nest generate controller example

This command will create a new controller file named “example.controller.ts” along with a corresponding unit test file.

Step 3

Defining Routes Open the “example.controller.ts” file in your preferred code editor. Inside the controller class, define a simple GET route that returns a JSON response:

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

@Controller('example')
export class ExampleController {
@Get()
getExample(): string {
return 'Hello, NestJS!';
}
}

Step 4

Running the Application To start your NestJS application, run the following command:

$ npm run start

This command will compile your TypeScript code and start the application. You should see a message indicating that the application is running successfully.

Step 5

Testing the Route Open your web browser and navigate to “http://localhost:3000/example". You should see the response “Hello, NestJS!” displayed on the page. Congratulations! You have successfully created a basic NestJS application with a functioning route.

Step 6

Adding a Service Services handle the business logic of your application. Generate a new service using the Nest CLI:

$ nest generate service example

This command will create a new service file named “example.service.ts” along with a corresponding unit test file.

Step 7

Injecting the Service into the Controller Open the “example.controller.ts” file again. Import the newly generated service and inject it into the controller’s constructor:

import { Controller, Get } from '@nestjs/common';
import { ExampleService } from './example.service';

@Controller('example')
export class ExampleController {
constructor(private readonly exampleService: ExampleService) {}
@Get()
getExample(): string {
return this.exampleService.getHello();
}
}

Step 8

Implementing the Service Open the “example.service.ts” file. Modify the service class to include a “getHello” method:

import { Injectable } from '@nestjs/common';

@Injectable()
export class ExampleService {
getHello(): string {
return 'Hello, NestJS Service!';
}
}

Step 9

Testing the Updated Application Restart your NestJS application by stopping it with Ctrl + C in the terminal and running npm run start again. Refresh your browser at "http://localhost:3000/example", and you should now see the response "Hello, NestJS Service!".

Conclusion

In this article, we walked through a step-by-step guide on how to get started with NestJS. By following along with the example, you learned how to set up a project, create controllers, define routes, add services, and inject them into controllers. This basic foundation will help you build more complex applications using NestJS. Explore the official NestJS documentation (https://docs.nestjs.com) to discover more features and dive deeper into the framework. Happy coding with NestJS!

--

--

Johnstyle

Expert en infrastructure cloud, en architecture micro-services et en solutions IoT, je suis spécialisé dans les technologies de Smart Building.