Nest.js - Introduction (Part 1)

Nest.js - Introduction (Part 1)

The popular Node.js framework to build server-side applications.

I've been learning Nest.js for the last few months and it has become my favourite Node.js framework. So, because of that, I decided to write a little introduction about Nest.js and how it works.

What is Nest.js?

First of all, if it's the first time you hear about Nest.js you may want to know what is and what you can do with it, so we'll start with that.

Nest.js is a framework to build server-side applications with Node.js that provide us with a complete architecture that allow us to create highly testable, scalable and easy to maintain projects. Also, Nest.js combines a lot of things about Object-oriented programming, Functional programming and Functional Reactive Programming.

Behind the scenes, Nest.js use Express (or Fastify) to make HTTP servers, but Nest.js is highly customizable that you can use whatever you want. Nest comes with a lot of integrated tools to create our project without complication, adding things for data validation, error handling, authentication, authorization, routing, filters and so on.

Project setup

The Nest team provide us with a nice tool called @nestjs/cli as its name explains itself it's a CLI tool that can do the recurring tasks for us when we're developing a project with Nest.

Let's start with the installation of that tool, open your terminal and write the following command using npm or yarn.

npm i -g @nestjs/cli
# or 
yarn global add @nestjs/cli

After the installation finishes (it may take a while) we can run the nest command to check that all of the things works fine. So, just run the following command in your terminal.

nest -v 

# output: v8.1.1

Once you run this command, you should be able to see the current version of Nest.js, don't worry if your version is the newest than mine.

So, we're good! Now we can create a Nest.js project, the only thing that we need to do is to use the nest command with the following options.

nest new your-project-name

This will create the full structure of a Nest.js project for us. After running that command, the CLI may ask you about what package manager you prefer (npm or yarn), select the one that you love.

Once the command finishes its process, we'll have a folder with the name of our project, and inside of that folder, we'll see the following structure.

├── src
│   ├── app.controller.spec.ts
│   ├── app.controller.ts
│   ├── app.module.ts
│   ├── app.service.ts
│   └── main.ts
├── test
├── node_modules
├── .eslintrc.js
├── .prettierrc
├── .gitignore
├── README.md
├── package.json
├── nest-cli.json
├── tsconfig.json
├── tsconfig.build.json
└── yarn.lock

First Steps

Well, at this point we already have created our project, so let's start writing some code. I want to explain all of the things from scratch, so let's delete all of the files inside of the src folder. And then, create a new file called main.ts inside of it.

main.ts

The main.ts file it's as their name explains itself the main file that executes Nest.js (or our project) to run the entire application.

const bootstrap = async () => {};
bootstrap();

In this async function, we'll initialize (or create) the instance of our application. You can rename the function to the name that you want without problems.

Well, an instance, but what instance?

Nest.js provide us with a class called NestFactory and a static method named create() that creates an instance of a Nest.js application, that will allow us to mount our project and be executed. This can be compared (or similar) to the process that we do when creating (or initialize) an application in express.

import { NestFactory } from '@nestjs/core';

const bootstrap = async () => {
  const app = await NestFactory.create(/* something here... */)
};

bootstrap();

We store the object that returns the create() static method of the NestFactory in a variable (this will allow us to set up some configuration, and use other tools). Right now we have a problem because the create() static method needs a param, that param is the Root module of our application. To fix that, let's create a file called app.module.ts inside of our src folder with the following code:

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

@Module({})
export class AppModule {}

Don't worry about this thing called module I'll explain it later in this series. Once we create this root module, let's include it in our main.ts file as follow:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

const bootstrap = async () => {
  const app = await NestFactory.create(AppModule);

  app.listen(5000);
};

bootstrap();

In the code above we just import our AppModule that we create before, and we just pass it into the create() method params. And also we have called the method listen() passing to it the value 5000 that represents the port, this method just create an HTTP listener to set up our HTTP server.

At this point, we can run our project and see that it's working fine.

To run our project in development mode (on watch mode) we just need to run the following command in our terminal.

npm run start:dev
# or
yarn start:dev

Once you run the command you will see something like this:

image.png

And that's it, our project it's ready to write more code and start to create our API using Nest.js.

Did you like it? See you in the next part of the series. 😊