前言
作为一名程序员,离不来英语,软件官方文档,顶会期刊和论文,基本都是英语。 而且英语是一门形式化语言,比如被动,现在、过去、将来等即就是时态语态可以从 英语表达的形式上体现出来。比较容易能看出来描述的主体的逻辑关系,这对于学习 程序概念学习是有益处。恰好最近正在学,也是探索和尝试一种学习英语的方式。
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