Skip to content

Routing And Request Handling

Defining a Controller ,Module ,Service:
Terminal window
// 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 auth

This 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 {}

@Controller('auth'): This decorator maps the controller to handle routes that start with /auth.

Creating Routes
auth.controller.ts
import { Controller } from '@nestjs/common';
@Controller('auth')
export class AuthController {
// Handle GET requests for /auth
@Get()
findAll() {
//Your logics here
return '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.