核心概念

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.

Nest的核心概念是提供结构系统,帮助开发只实现最大分层,并提高应用程序的抽象性。

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

应用程序构建块 Nest有三种基本的应用程序构建块

  1. 模块。
  2. 控制器。
  3. 组件。

模块

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({}) 装饰器的类。该装饰器提供元数据,框架使用该元数据组织应用程序结构。 以下是一个简单模块的所有可用属性:

@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.

默认情况下,模块封装每一个依赖,也就是说模块只能在其内部使用组件/控制器。 我们可以将组件实例导出(只有组件可以被导出),这样模块之间就可以共享组件实例了。

控制器

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

控制层负责处理传入的HTTP请求。控制器是一个带有@Controller()装饰器的类。

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

组件

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).

几乎所有的事物都可以被看作一个组件--Service, Repository, Provider等。可以通过构造函数将组件注入到控制器或者另一个组件中。

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

Learn more about those building blocks in next sections.

results matching ""

    No results matching ""