Core Concepts

The core concept of Nest is to provide an architecture, which helps developers to accomplish maximum separation of layers and increase abstraction in their applications.

It has enormous potential, but does not solve problems for you. It is not just a set of prepared classes, with some behavior. It is an idea, which shows you how to organize modern application and enable you to focus on application logic.

Application building blocks

We have three basic application building blocks in Nest:

  • Modules
  • Controllers
  • Components

Modules

Module is a class with @Module({}) decorator. This decorator provides metadata, which framework uses to organize application structure.

Simple module with all available properties:

@Module({
    modules [ TestModule ],
    controllers: [ TestController ],
    components: [ TestComponent ],
    exports: [ TestComponent ]
})
export class ApplicationModule {}

By default, modules encapsulate each dependency. It means, that it is not possible to use its components / controllers from another module.

To allow different modules to share same instance of component (only components can be exported), we could simply exports it.

Controllers

The Controllers layer is responsible for handling incoming HTTP requests. Controller is a simple class with @Controller() decorator.

@Controller()
class UsersController {
    @Get('users')
    getAllUsers(@Res() response) {
        res.status(201).json({});
    }
}

Components

Almost everything is a component - Service, Repository, Provider etc. and they might be injected to controllers or to another component by constructor (as in Angular).

@Component()
class UsersService {
    getAllUsers() {
        return [];
    }
}

Learn more about those building blocks in next sections.

results matching ""

    No results matching ""