Getting started with NestJS
Tags: typescript, node, javascript, nestjs
Published: 2024-05-28 19:26:12

Love to work with you, You can hire me on [Upwork](https://www.upwork.com/freelancers/~011463d96b5b87d7ff).

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.

```bash
npm i -g @nestjs/cli
nest new nestjs-series
```

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.

```bash
src
├── entities
├── main.ts
└── modules
    ├── app
    ├── auth
    ├── config
    ├── database
    ├── mail
    ├── media
    └── user
```

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

```json
POST /v1/auth/email/register

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

// response code
201 Created Successfully
```

2. Verify email address

```json
POST /v1/auth/email/verify

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

// response code
200 Verified Successfully
```

3. Login user

```json
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"
}
```

4. Send verify email

```json
POST /v1/auth/email/send-verify-email

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

// response code
204 
Sent Verification mail.
```

5. Request reset password

```json
POST /v1/auth/email/reset-password-request

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

6. Reset Password

```json
POST /v1/auth/email/reset-password

// body
{
  "password": "Password@123",
  "reset_token": "vhsbdjsdfsd-dfmsdfjsd-sdfnsdk"
}
```

7. Log out user
```json
GET /v1/auth/logout // authenticated route
```

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
```bash
npm i --save class-validator class-transformer
```

2. 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.

```bash
npm install --save @nestjs/swagger
```
3. 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.

```bash
npm install --save @nestjs/typeorm typeorm pg
```
4. 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.
```bash
npm i @nestjs/platform-express // it should be installed with nest new command
```

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](https://github.com/danimai-org/nestjs-series).