Defining Routes and Endpoints
Свайпніть щоб показати меню
Routes define how your backend responds to different requests. Each route is connected to a specific HTTP method and path.
In Nest.js, routes are created inside controllers using decorators.
Here is an example:
import { Controller, Get, Post } from '@nestjs/common';
@Controller('users')
export class UsersController {
@Get()
getAllUsers() {
return 'Get all users';
}
@Post()
createUser() {
return 'Create user';
}
}
@Controller('users'): sets the base route;@Get(): handles GET requests to/users;@Post(): handles POST requests to/users.
Each method inside the controller becomes an endpoint.
You can also define routes with additional paths:
@Get('profile')
getProfile() {
return 'User profile';
}
This creates the route:
/users/profile
Each route responds to a specific combination of:
- HTTP method;
- URL path.
This allows you to define different behaviors for the same route depending on the request type.
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Defining Routes and Endpoints
Routes define how your backend responds to different requests. Each route is connected to a specific HTTP method and path.
In Nest.js, routes are created inside controllers using decorators.
Here is an example:
import { Controller, Get, Post } from '@nestjs/common';
@Controller('users')
export class UsersController {
@Get()
getAllUsers() {
return 'Get all users';
}
@Post()
createUser() {
return 'Create user';
}
}
@Controller('users'): sets the base route;@Get(): handles GET requests to/users;@Post(): handles POST requests to/users.
Each method inside the controller becomes an endpoint.
You can also define routes with additional paths:
@Get('profile')
getProfile() {
return 'User profile';
}
This creates the route:
/users/profile
Each route responds to a specific combination of:
- HTTP method;
- URL path.
This allows you to define different behaviors for the same route depending on the request type.
Дякуємо за ваш відгук!