---
# System prepended metadata

title: NestJS

---

# NestJS
What is Nest JS?
It is a framework for building efficient, scalable Node.js server-side applications. It fully support `TypeScript`.

## Controller
What is a controller?
It is responsible for listening for request and make a response. In short, controller is responsible for creating the api end point and its response body.

`users.contoller.ts`
```typescript=
import { Controller, Get, POST } from '@nestjs/common';

@Controller('users')
export class UsersController {
    @Get()
    @HttpCode(200)
    findAll(): string {
        return `Return all the list of users`;
    }
    
    @Get(:id)
    @HttpCode(200)
    findOne(@Param('id') id: number): string {
        return `Return #${id} User`;
    }
    
    @POST()
    @HttpCode(201)
    create(): string {
        return 'Adds a new user to the User List';
    }
}
```
In short, if make an  **GET** request `/api/users` it will return something like this.

```json=
{
    "users": [
        {
            "id": 1,
            "name": "Lim",
            "job": "NestJS Developer"
        },
        {
            "id": 2,
            "name": "German",
            "job": "Senior Go Developer"
        },
        {
            "id": 3,
            "name": "Swan",
            "job": "Senior Salesforce Developer"
        },
        {
            "id": 4,
            "name": "Luke",
            "job": "Senior Bento Developer"
        }

    ]
}
```

And if we make a **GET** request `/api/users/1` it will return something like this.

```json=
{
    "users": {
        "id": 1,
        "name": "Lim",
        "job": "NestJS Developer"
    }
}
```

If we make a **POST** request `/api/users` .
```json=
{
    "name": "Hung",
    "job": "Dev Manager"
}
```

And the response would be. 
```json=
{
    "name": "Hung",
    "job": "Dev Manager",
    "id": "5",
    "createdAt": "2021-05-11T02:19:17.688Z"
}
```

## Providers
What is a providers?
Providers are a type of a class that uses a form of complexity or logic. Think of it a Service Provider that interacts with a database.

`user.interface.ts`
```typescript=
export interface User {
    name: string;
    job: string;
}
```

`users.service.ts`
```typescript=
import { Injectable } from '@nestjs/common';
import { User } from './interfaces/user.interface';

@Injectable()
export class UsersService {
    private readonly users: User[] = [];
    
    create(user: User){
        this.users.push(user);
    }
    
}
```

## Modules
What is a modules?
Modules provides metadara that NestJS uses to organize the app structure.

`users.module.ts`
```typescript=
import { Module } from '@nestjs/common';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';

@Module({
    controllers: [UsersController],
    providers: [UsersService],
});
export class UsersModule {}
```

Then we need to add this module in the root folder.

`app.module.ts`
```typescript=
import { Module } from '@nestjs/common';
import { UsersModule } from './users/users.module';

@Module({
    imports: [UsersModule],
})
export class AppModule {}
```

The `@Module()` decorator takes one object whose properties descrive the module:

- `imports`: Modules that will be use in app.
- `exports`: To ensure other modules can be used throughout the app.
- `controllers`: Set of controllers defined in this module which have to be instantiated.
- `providers`: All services and providers within the module will be place inside

## Interceptor
What is Interceptor?
It is a set of middleware that lets you see the request that goes into the application.

## Guard
What is a Guard?
It's a middleware that used for authentication and authorization.
It can only return true or false.

## Pipe
What is a Pipe?
It's a middleware that sits between client and the controller.
Mostly used for transforming data, before they go to the controller.

## DTO (Data Transfer Object)
What is DTO?
It defines how data will be sent over the network.

## Interfaces
What is Interfaces?
It is used for type-checking.








