0
0
NestjsHow-ToBeginner ยท 3 min read

How to Use @Get Decorator in NestJS: Simple Guide

In NestJS, use the @Get decorator to define a method that handles HTTP GET requests for a specific route inside a controller. Place @Get('route') above a method to respond when that route is accessed via GET.
๐Ÿ“

Syntax

The @Get decorator is used above a controller method to specify it handles HTTP GET requests. You can provide an optional route path as a string inside the parentheses. If no path is given, it defaults to the controller's base route.

  • @Get(): Handles GET requests to the controller's base path.
  • @Get('subroute'): Handles GET requests to /basepath/subroute.
typescript
import { Controller, Get } from '@nestjs/common';

@Controller('items')
export class ItemsController {
  @Get() // Handles GET /items
  findAll() {
    return 'Return all items';
  }

  @Get('special') // Handles GET /items/special
  findSpecial() {
    return 'Return special items';
  }
}
๐Ÿ’ป

Example

This example shows a simple NestJS controller using @Get to respond to GET requests at two routes: the base /products and a sub-route /products/featured. When you visit these URLs, the server returns the corresponding string.

typescript
import { Controller, Get } from '@nestjs/common';

@Controller('products')
export class ProductsController {
  @Get()
  getAllProducts() {
    return ['Apple', 'Banana', 'Cherry'];
  }

  @Get('featured')
  getFeaturedProducts() {
    return ['Banana'];
  }
}
Output
GET /products --> ["Apple", "Banana", "Cherry"] GET /products/featured --> ["Banana"]
โš ๏ธ

Common Pitfalls

Common mistakes when using @Get include:

  • Forgetting to add @Controller on the class, so routes won't register.
  • Not importing the controller in the module, causing routes to be unreachable.
  • Using @Get without parentheses when you want to specify a path (must use @Get('path')).
  • Defining multiple methods with the same route path, which causes conflicts.
typescript
import { Controller, Get } from '@nestjs/common';

// Wrong: Missing @Controller decorator
export class WrongController {
  @Get('test')
  test() {
    return 'This will not work';
  }
}

// Correct usage
@Controller('test')
export class CorrectController {
  @Get('test')
  test() {
    return 'This works';
  }
}
๐Ÿ“Š

Quick Reference

Summary tips for using @Get in NestJS:

  • Use @Get() for the controller's root GET route.
  • Use @Get('path') to handle GET requests on a sub-route.
  • Always decorate your class with @Controller('basepath').
  • Ensure your controller is included in the module's controllers array.
  • Methods decorated with @Get should return data or responses for the client.
โœ…

Key Takeaways

Use @Get decorator above controller methods to handle HTTP GET requests.
Provide an optional route string inside @Get() to specify the GET path.
Always decorate your class with @Controller to define the base route.
Register your controller in the module to make routes active.
Avoid duplicate route paths in the same controller to prevent conflicts.