前言
作为一名程序员,离不来英语,软件官方文档,顶会期刊和论文,基本都是英语。 而且英语是一门形式化语言,比如被动,现在、过去、将来等即就是时态语态可以从 英语表达的形式上体现出来。比较容易能看出来描述的主体的逻辑关系,这对于学习 程序概念学习是有益处。恰好最近正在学,也是探索和尝试一种学习英语的方式。
Nest.js Overview
We'll take an overview of Nest.js
and
look at
the core concepts
- that you'll need to build a Nest.js application.
知识点
- take an overview
- 在这个语境下,
take
本身没有拿取的意思,而是与后面的名词组合来形成一个特定的意思 - 意思是:对某事或某物进行一个总体的、概括性的查看或审视
- 近似于
- have a general look
- get a broad perspective
- 近似于
take
可与多种名词搭配,形成各种意思- take a look: 看一下
- take a break: 休息一下
- take a shower: 洗个脸
- take a chance: 冒个险
- 在这个语境下,
- build a Nest.js application
- 构建一个...应用
Controllers
-
Controllers in Nest
- are responsible for
- handling
incoming requests
- and
returning responses
to the client.
-
Nest will route
incoming requests
tohandler functions
- in controller classes.
-
We use the
@Controller decorator
- to create a controller class.
翻译
Nest中的Controller
负责(resposible for)处理传入请求(incoming requests)并向客户端返回响应(returning responses)
Nest会将**传入请求(incoming request)**路由(route)到控制器类(Controller)中的处理函数(handler function)
我们使用@Controller()
装饰器(decorotor)来创建一个控制器类
示例
import { Controller, Get } from '@nestjs/common'
@Controller
export class EnteryController {
@Get
index(): Entry[] {
const entries: Entry[] = this.entriesService.findAll()
return entries
}
}
知识点
- Controllers in Nest
- 在...的...
-
responsible for
- phrasal verb
- 描述某事或某物的职责或功能
- handling incoming request and returning response
- 处理传入请求和返回响应
- route incoming requests
- 路由传入的请求
- 描述如何处理(route)请求
- handler function
- 处理函数
- controller classes
- 控制器类
@Controller
decorator- 控制器装饰器
Provider
Providers in Nest
are used tocreate
- services
- factories
- helper
- and more
- that can be injected into controllers
- and other providers
- using Nest's built-in dependency injection
The @Injectable() decorator
is used to create a provider class.
翻译
Nest中的provider
用于创建services
、factories
、helpers
这些services
、factories
、helpers
可以使用Nest内置的依赖注入到控制器和其他provider
中。
@Injectable()
装饰器用于创建a provider class
示例
我们博客应用中的AuthenticationService
事一个注入并使用UsersService
组件的提供程序
@Injectable
export class AuthenticationService {
constructor(private readonly userService: UserService) {}
async validateUser(payload: { email: string; password: string;}): Promise<boolean> {
const user = await this.userService.findOne({where: {email: payload.email }})
return !!user
}
}
知识点
-
are(be) used to
- 被用于...
- Providers in Nest
- 在...的...
-
used to create
- 表示用于创建或制造某物
- used to create a provider class
- service, factories, helpers, and more
- 列举...
-
can be injected into
- 表示某物可以被注入到其他物体中
- Nest's built-in dependency injection
- built-in: ...内置...
- dependency injection: 依赖注入
Modules
A Nest.js application is organized into modules
.
Every Nest.js application will have a root module
-
In a small application
- this may be the only module.
-
In a larger application
- it makes sense to organize your application into multiple modules
- that split up your code into features and related capabilities
- it makes sense to organize your application into multiple modules
-
A module in Nest.js
-
is a class
- with a @Module() decorator.
-
is a class
-
The @Module() decorator
- takes a single object
- that describes module using the following properties.
- components
- controllers
- imports
- exports
- that describes module using the following properties.
- takes a single object
-
components
- The components to be instantiated
- that may be shared across this module
- and exported to be available to other modules
- The components to be instantiated
-
controllers
- The controllers that are created by this module
-
imports
- The list of modules to import that export components
- that are requires in this module
- The list of modules to import that export components
-
exports
- The list of components from this module
- to be made available to other modules
- The list of components from this module
-
The AppModule imports the modules
- that are needed for the application.
-
The root module in our application
- doesn't need to have any
exports
- since no other modulers import it
- doesn't need to have any
-
The root module also doesn't need to have any
exports
- since no other modules import it
-
The root module doesn't have any
conponents
orcontrollers
- as these are all organized within the sub-modules they are related to
-
Modules in Nest.js are singletons by default.
- This means that you can share the same instance of an exported component
翻译
每个Nest.js应用被组织成模块。
每个Nest.js都有一个根模块
在小型应用中,根模块可能是唯一的模块
在大型应用程序中,应用程序被组织成多个模块,这些模块将代码分割成不同的功能和相关能力
Nest.js中的模块是带有@Module()
装饰器的类。
-
@Module()
装饰器采用一个使用以下属性描述模块的对象components
:- 要实例化的组件可以跨该模块共享并导出以供其他模块使用
controllers
:- 该模块创建的控制器(controllers)
imports
:- 要导入的模块列表,该模块中所需的组件
exports
:- 该模块可供其他模块使用的组件列表
-
The AppModule(the root module)
- 导入应用程序所需的模块(modules)
- 根模块不需要任何导出,因为没有其它模块导入它
- 也没有任何组件(
components
)或(controllers
),因为它们都组织在与其相关的子模块(sub-modules)中
示例
在根模块AppModule
中包含多个模块处理认证、评论、数据库访问、博客条目和用户
@Module({
components: [],
controllders: [],
imports: [
DatabaseModule,
AuthenticationModule.forRoot('jwt'),
UserModule,
EntryModule,
CommentModule,
UserGatewayModule,
CommentGatewayModule
],
exports: [],
})
export class AppModule implements NestModule {}
The EntryModule
@Module({
components: [entryProvider, EntryService],
controllers: [EnteryController],
imports: [],
exports: [EntryService]
})
export class EntryModule implements NestModule {}
知识点
-
is organized into
- 被动语态的动词短语
- 某物或某事被安排活组织成某种形式或结构
- 整体 be organized into 部分
-
to organize ... into ...
- to organize 整体 into 部分
-
split up
- phrasal verb
- 分割或分离成若干部分
- 将一群人、物品、任务分成几个小组或部分
- to split up 整体 into 部分
- The teacher decided to split up the students into small groups for the project
-
features and related capabilities
- 应用程序的功能和相关能力
-
module in Nest.js
- 在...的...
-
@Module
decorator -
take a single object
- 描述接受一个对象的动作
-
components to be instantiated
- 需要实例化的组件
-
available to other modulers
- 对其他模块可用
-
controller that are created by this module
- 控制器被模块创建
-
list of modules to import
- 需要导入的模块列表
-
list of components from this module
- 来自模块的组件列表
-
components that are required in this module
- 在此模块中所需的组件
-
since ...
- 因为
-
as ...
- 因为
Bootstrapping(自举)
- Every Nest.js application**
- needs to be bootstrapped.
- This is done by byusing the NestFactory
- to create the root module and calling the listen() method.