Services in Nest js

Services are used to perform business logics and communicate with database.there is simple example of services and controller nest js.

Users.service.ts

import { Injectable } from "@nestjs/common";

interface UserDTO {
    email: string,
    name: string
}

@Injectable()
export class UserService {
  users = [];

    createUsers(user: UserDTO){
        this.users.push(user)
        return user
    }

    getUsers() {
        return this.users;
    }

}

this service has two functions create user and get all users

Users.controller.ts

import { Body, Controller, Get, Header, HttpCode, HttpStatus, Param, Post, Query, Req } from "@nestjs/common";
import { Request } from "express";
import { UserService } from "./users.service";

interface userDTO {
    name: string,
    email: string
}

@Controller('/users')
export class UsersController {
    constructor(private userService: UserService) {

    }
    @Post('/create')
    @HttpCode(201)
    createUser(@Body() data: userDTO) {
        const data1 = this.userService.createUsers(data);
        console.log(data1)
        return 'User Created Successfully'
    }

    @Get()
    @HttpCode(200)
    findUsers() {
        const data1 = this.userService.getUsers();
        return data1
    }
}

Users.module.ts

import { Module } from '@nestjs/common';
import { UsersController } from './users.controller';
import { UserService } from './users.service';

@Module({
  imports: [],
  controllers: [UsersController],
  providers: [UserService],
})
export class AppModule {}