Getting Started With NestJS [Beginners Guide]
2021-02-26 | 5 min read

Getting Started With NestJS [Beginners Guide]

Damir Ljubičić

Web Developer

As backend developers, we go through dozens of frameworks and test out a good number of them throughout our careers. The last one I tested was NestJS - so if you’re looking for a new framework and want to give NestJS a try, keep reading, and I’ll show you how.

NestJS is a progressive framework for building efficient, reliable, and scalable server-side applications. Or, to put it more simply, it adds another level of abstraction to NodeJS and it helps structure your code and project. I found it interesting because it fully supports Typescript and uses ExpressJS, which I use on a daily basis anyway. You can find more on the official website of NestJS.


First things first, to use NestJS, you need to install Nest CLI (Command Line Interface). You can do that using npm (node package manager). If you’re not familiar with npm, it is a package manager for the Javascript programming language.
Once you are set and have npm installed, you want to run the following command:

With this command line, you will install the NestJS Command Line Interface. If you want to install it globally on your machine, use -g.Once the installation is complete, you can use our newly installed Nest CLI and create a new project using the following command:

Once this command executes, the default project will be downloaded into a folder named nestjs_hw. Upon command execution, you will see an option to choose between two package managers: yarn and npm. I prefer npm, so I will be using that. 

In the following screenshot, you can see what type of structure NestJS uses - the src (source) folder is the most important one for us. Also, it’s important to notice that NestJS uses the MVC(Model-View-Controller) structure, which enables us to work in parallel as a team.

Structure and code

Aside from the structure, we will take a closer look at the code itself and what is generated. First and foremost, let’s look at main.ts.

This is the “main” file and starting point for every application. We 
have a new instance of Nest application created with NestFactory.create( ). 

After importing the necessary modules, we are implementing a 
bootstrap function that is marked as async. NestFactory.create 
is called with AppModule as an argument. 
Since we have a “listen” method that’s returning a promise when
 our server starts, we will use await here. By using NestFactory.create(), we create a new instance of the Nest application. After importing the necessary modules and bootstrap function, we need to call NestFactory.create with AppModule as an argument. 

Let’s take a closer look at app.module.ts

This file contains the application root module implementation.

 The object with three properties is passed to the Module decorator.
 Every controller that needs to be a part of AppModules needs
 to be an array that is passed inside the controller array, as every 
service needs to be passed to the provider’s property. 
 To pass controllers, we need to put them in an array called controllers, and the same is needed for providers.

Next, we can take a look at app.controller.ts

This contains the implementation of a basic NestJS controller and here we have only one route -

 a simple implementation of the GET route. To make a class A controller 
we will add a @Controller decorator. 
By using the dependency injection, we inject AppService into the 
AppController. The last thing we need to do is implement the getHello
 method to implement a default route.

Now check app.service.ts

Services have a basic service implementation.

To make AppService a class, we need to add @Injectable before 
that class. This Service class implementation has only one method - the getHello method,
 which just returns the ‘Hello World!’ text. 

After looking at the code and the folder-file structure, you can start a project and see what happens.

To start the NestJS project you just need to use the standard command for starting projects through the script.

For anyone that is unfamiliar with this, what we did is: we used a script defined in package.json, started our project with the command npm run start, and NestJS handled the rest.

The good and the bad

Four reasons you should give NestJS a try:

1. The developer community - The developer community you will encounter is very supportive. Whenever you face a problem you can’t seem to solve, you can reach out to them either through GitHub, Stack Overflow, or NestJS official discord. Even the creator of NestJS is active on those platforms, which is pretty cool.

2. Typescript support - NestJS supports both vanilla Javascript and Typescript superset, so whatever you prefer, you got it. For me, it’s Typescript, because of its flexibility.

3. Modularity - This is maybe the best thing NestJS does. Modularity means that once you create a module, you can reuse it in every other NestJS service you will build in the future. For example, you have a login module you use in every project - with NestJS, after building it once, you can reuse it in every single future project and save yourself some time.

4. Creating API documentation and status monitoring - NestJS has excellent solutions for creating API documentation and status monitoring. With certain modules, such as the swagger module or nest status monitor, you can quickly get a platform that has error logging, status monitoring, and API documentation.

Three reasons you might reconsider using NestJS:

1. Command Line Interface - the main problem with CLI is it has minimal features, which leads to people using starter templates created by others and creating modules with needed configuration.

2. Error messages - the biggest problem with error messages, which people complain on Stack Overflow, come from the fact that they often give information about what happened but not so often about why it happened.

3. Documentation - here we’re talking about the official documentation about possibilities on the NestJS website. Even though it’s highly useful, it lacks examples in some areas which would help a beginner a lot. 

Should I stay or should I go?

To wrap up, NestJS was definitely not created for everyone. In my opinion, it’s for teams that have a larger number of backend developers working on similar projects, that could really make use of the modularity option. So, if you’re working on a team like that, I say go for it! And good luck!

Like what you read?Go on, share it with friends!

Damir Ljubičić

Web Developer
Damir is a Backend developer at COBE. He enjoys working on interesting projects surrounded by great people, and in his free time, he likes to play board games, read books and watch Netflix.

Let's turn your idea into reality

Save money, time and energy and book the entire team today.