DEV Community

depak379mandal
depak379mandal

Posted on

Getting started with NestJS

Love to work with you, You can hire me on Upwork.

You can just take a look into NestJS doc for more basics but yeah we will be covering all over the tutorial series. It is a practical guide to understand whole NestJS framework for beginner and intermediate to advanced.

Before anything else we need a starting point and NestJS covered that for us. We can use their CLI tool to create new project. First, we need to install nest CLI as global command. You can use the below command to install and start your own NestJS project.

npm i -g @nestjs/cli
nest new nestjs-series
Enter fullscreen mode Exit fullscreen mode

Now we have a project, just open it up your liked text editor, I am using VS Code. Defining a layout/architecture of project is very crucial part, you can see src folder inside just create this particular folder and file structure that we will be using for entire project.

src
├── entities
├── main.ts
└── modules
    ├── app
    ├── auth
    ├── config
    ├── database
    ├── mail
    ├── media
    └── user
Enter fullscreen mode Exit fullscreen mode

Let me describe them one by one, entities will have all the models/entities that will take care of our communication with DB and those will be injected accordingly into services as used for. We will be discussing more on Dependency Injection and services and many more things coming in this series.
What we want to achieve

We want to achieve a boilerplate or small project that will be guided to understand whole NestJS frameworks. NestJS is more like library instead of framework not as framework as it does provide you very much flexibility in terms of everything you wanted to achieve in any way.

So what we want to create is very important to even start any project. Because without what you want to build, doing anything is time taking and dumping things to create a pile of bugs.

Let us start with some API doc, what we wanted to achieve in this series. I will not be able to write e2e test cases as it will be slowing down our series progress.

Here are our APIs’ and there documentation to be made, we can first define the Auth API docs that we can start our work with

  1. Register a user
POST /v1/auth/email/register

// body
{
  "email": "example@danimai.com",
  "password": "Password@123",
  "first_name": "Danimai",
  "last_name": "Mandal"
}

// response code
201 Created Successfully
Enter fullscreen mode Exit fullscreen mode
  1. Verify email address
POST /v1/auth/email/verify

// body 
{
  "verify_token": "vhsbdjsdfsd-dfmsdfjsd-sdfnsdk"
}

// response code
200 Verified Successfully
Enter fullscreen mode Exit fullscreen mode
  1. Login user
POST /v1/auth/email/login

// body
{
  "email": "example@danimai.com",
  "password": "Password@123"
}
// response code
200 Logged in Successfully

// response body
{
  "auth_token": "vhsbdjsdfsd-dfmsdfjsd-sdfnsdk"
}
Enter fullscreen mode Exit fullscreen mode
  1. Send verify email
POST /v1/auth/email/send-verify-email

// body
{
  "email": "example@danimai.com"
}

// response code
204 
Sent Verification mail.
Enter fullscreen mode Exit fullscreen mode
  1. Request reset password
POST /v1/auth/email/reset-password-request

// body 
{
  "email": "example@danimai.com"
}
Enter fullscreen mode Exit fullscreen mode
  1. Reset Password
POST /v1/auth/email/reset-password

// body
{
  "password": "Password@123",
  "reset_token": "vhsbdjsdfsd-dfmsdfjsd-sdfnsdk"
}
Enter fullscreen mode Exit fullscreen mode
  1. Log out user
GET /v1/auth/logout // authenticated route
Enter fullscreen mode Exit fullscreen mode

Now as we go ahead we need more of brain mapping structure that we wanted to create modules for. I also wanted to explain the structure that we are going to use. So the list of what we require are and what we are going to

  1. validation

class-validator and class-transformer those are two libraries we are going to use for validation, so we can just run

npm i --save class-validator class-transformer
Enter fullscreen mode Exit fullscreen mode
  1. Swagger doc

We have very cool library from NestJS that handles all our requirement by attaching Swagger decorator in validator classes, and we can just use that as extension not as whole separate setup that requires maintenance. You can run below command to install swagger library by NestJS.

npm install --save @nestjs/swagger
Enter fullscreen mode Exit fullscreen mode
  1. SQL ORM (TypeORM)

We are going to use TypeORM as it supports very well with NestJS, but I would suggest Drizzle or Knex Or anything else that are very fast and allow us more control over raw SQL. But you also have to remember TypeORM is not enemy, you don’t have to migrate to any other ORM or library if you don’t face any critical issues. To install TypeORM use the below command. The command includes TypeORM, TypeORM wrapper for NestJS and pg driver for PostgreSQL.

npm install --save @nestjs/typeorm typeorm pg
Enter fullscreen mode Exit fullscreen mode
  1. Platform (Express)

Indeed, NestJS is a separate framework, but It uses platform library like express or Fastify to use to run everything. NestJS actually provides its own wrapper to run everything. We will go forward with express.

npm i @nestjs/platform-express // it should be installed with nest new command
Enter fullscreen mode Exit fullscreen mode

Above are basic library we need to proceed Or to just gain more idea what are we dealing with. In the next article, we will set up things with TypeORM and create some commands to run migration. We will go over each of the things mentioned above, So see you in the next.

Code for every article are provided in this repo.

Top comments (0)