πŸš€ Nestjs BareBones Controllers & ReqObjects

🧩 features

  • typescript(disciplined)
  • oops(SOLID principles)
  • scalable/testable/loosely coupled production grade architecture boilerplate setup
  • πŸ‘Ά steps(understanding the blueprint)

  • npm i -g @nestjs/cli

                # cmd line
                nest -v
    
                # config your nest backend
                nest new nest-basics-api
    
                # choices
                npm or yarn or pnpm
    
  • **app.module.ts is the root of the api**

    **everything is imported inside app.module and then this is exported and bootstrapped inside main.ts to create the nest backend**

    πŸ”§ building the api

    1. πŸ“© Controllers

    **Handles incoming request from client and sends back response**

    ** ✏ never write the buisness logic inside the controllers**

  • In nest we define controllers via decorators that provide meta data about what functionality a particular code block will have
  • **the access decorators import @nestjs/common**

    Controllers
                        |- student.controller.ts
    
                # student.controller.ts
                import { Controller } from @nestjs/common
    
                # Controller will tell nest that this is a controller based class
    
                @Controller('students')
                class StudentController {
    
                }

    **every single route inside studentController is going to start as /students**

    **it can be specified with a @Get() decorator that this is a get request**

    @Get()
                getStudents(){
                    // return all students data
                }

    **make sure to import the student.controller.ts inside the app.module.ts**

    βœ” running dev server first time

    npm run start:dev
                    # make sure the main.ts is just under the src directory
  • nested routes like /students/:studentId

                @Get('/:studentId')
                getStudentById(){
                    return "Get Student By Id"
                }
    
  • πŸ±β€πŸš€πŸ±β€πŸš€πŸ±β€πŸš€TIRED OF REPETATIVE CONTROLLERS SETUPπŸ±β€πŸš€πŸ±β€πŸš€πŸ±β€πŸš€

    Creating controllers through nestcli πŸ±β€πŸ‘€

  • https://docs.nestjs.com/controllers

                nest g controller <controllerName> --no-spec
                # only add --no-spec flag if you dont need unit test file for this controller
    
  • best practice is to put similir prefix routes in seprate controller

    # to run tests
                    npm run test:watch

    2. Request Objects (extracting pieces of info from request like params)

    make use @param () decorator in Nest for GET

    # student object in student ID
                    @Get('/:studentId')
                    getStudentById(
                        @Param () params: {studentId: string}
                    ) {
                        console.log(params)
                        return 'Get Student By Id';
                    }
  • further it can be simplified while the required params can be destructured at the time of decorator defination

                # @Param('destructuredObjectFromParams')
                @Param ('studentId') studentId: string
                console.log(studentId)
    
  • make use @Body () decorator in Nest for POST

    @Post()
                    createStudent(
                        @Body() body
                    ) {
                        return `Create's New Student with details\n ${JSON.stringify(body)}`;
                    }

    use @Body & @Parma together for PUT

    @Put('/:studentId')
                    updateStudentById(
                        @Param('studentId') studentId: string,
                        @Body() body
                    ) {
                        return `Update's\n student id: ${JSON.stringify(studentId)}\n with new data ${body}`;
                    }

    Further their are @Query, @Session, @Next and @ip() that can be useful refer- https://docs.nestjs.com/controllers#request-object

    @Put('/:studentId')
                    updateTeacherOfStudentById(
                        @Param('teachersId') teachersId: string,
                        @Param('studentId') studentId: string,
                        @Ip() clientIP: string 
                    ) {
                        return `clientIP: ${clientIP}\nUpdate's Teacher with ID: ${teachersId}\n Associated To Student With Id: ${studentId}`;
                    }

    πŸ“š refferences

    29

    This website collects cookies to deliver better user experience

    πŸš€ Nestjs BareBones Controllers & ReqObjects