NestJS: Exploring the @nestjs/common Module

Introduction to the @nestjs/common Module

The @nestjs/common module is a core module of NestJS that provides a set of common features and utilities for building scalable and maintainable server-side applications. It includes decorators, providers, and exports that can be used to enhance the functionality of your NestJS application.

Exploring Decorators

NestJS provides several decorators in the @nestjs/common module that allow you to annotate your classes and methods in order to define various behaviors and configurations. Here are some commonly used decorators in this module:

@Controller()

The @Controller() decorator is used to define a class as a controller, which contains the route handlers for a specific set of HTTP requests. For example:

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

@Controller('users')
export class UsersController {
  @Get()
  findAll(): string {
    return 'Get all users';
  }
}

@Get(), @Post(), @Put(), @Delete(), etc.

These decorators are used to define route handlers for specific HTTP methods. For example:

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

@Controller('users')
export class UsersController {
  @Get()
  findAll(): string {
    return 'Get all users';
  }

  @Get(':id')
  findOne(): string {
    return 'Get user by ID';
  }
}

@Param() and @Query()

These decorators are used to extract route parameters and query parameters from the request. For example:

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

@Controller('users')
export class UsersController {
  @Get(':id')
  findOne(@Param('id') id: string, @Query() query: any): string {
    return `Get user with ID ${id}, query: ${JSON.stringify(query)}`;
  }
}

Understanding Providers

The @nestjs/common module also provides various providers that can be used to define and configure the dependencies required by your application. Providers are used for dependency injection, allowing you to easily manage the lifecycle and scope of your dependencies.

Services

A service is a class annotated with the @Injectable() decorator, which indicates that it can be injected as a dependency into other classes. For example:

import { Injectable } from '@nestjs/common';

@Injectable()
export class UserService {
  findAll(): string[] {
    return ['John', 'Jane', 'Doe'];
  }
}

Controllers as Providers

In NestJS, controllers can also act as providers. This means that the dependencies required by a controller can be injected into it using constructor injection. For example:

import { Controller, Get, Inject } from '@nestjs/common';
import { UserService } from './user.service';

@Controller('users')
export class UsersController {
  constructor(private readonly userService: UserService) {}

  @Get()
  findAll(): string[] {
    return this.userService.findAll();
  }
}

Exported Members

The @nestjs/common module also exports various utilities and classes that can be helpful in building NestJS applications. Some notable exports include the HttpException class for handling HTTP errors, the NestFactory class for creating instances of your NestJS application, and the ModuleRef class for managing module instances.

Conclusion

In this article, we explored the @nestjs/common module of NestJS and learned about the decorators, providers, and exports provided by this module. By utilizing these features, you can build robust and scalable applications with NestJS.

Remember to check out the official NestJS documentation for more details on the @nestjs/common module and its functionalities.

Happy coding with NestJS!