Routing And Request Handling
Defining a Controller ,Module ,Service:
// creating module nest generate module auth // or nest g mo auth
// creating controller nest generate controller auth //or nest g co auth
// creating service nest generate service auth //or nest g s authThis will create module,controller,service under auth/ directory.
import { Module } from '@nestjs/common';import { AuthController } from './auth.controller';import { AuthService } from './auth.service';
@Module({controllers: [AuthController],providers: [AuthService],})export class AuthModule {}import { Controller } from '@nestjs/common';
@Controller('auth')export class AuthController {}import { Injectable } from '@nestjs/common';
@Injectable()export class AuthService {}@Controller('auth'): This decorator maps the controller to handle routes that start with /auth.
Creating Routes
import { Controller } from '@nestjs/common';
@Controller('auth')export class AuthController {// Handle GET requests for /auth@Get()findAll() { //Your logics herereturn 'display all values';}
}@Get() : This decorator is used to handle GET requests. If no argument is passed, it handles the root of the controller path (/auth).
findAll() is the function that execute when /auth roue is called.