Skip to content

Commit

Permalink
更新6.0说明
Browse files Browse the repository at this point in the history
  • Loading branch information
zuohuadong committed Mar 28, 2019
1 parent ac02007 commit f440e0b
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 35 deletions.
3 changes: 2 additions & 1 deletion 4.6/_navbar.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- 版本
- [4.6](4.6/)
- [5.0](5.0/)
- [5.0](5.0/)
- [6.x](6/)
3 changes: 2 additions & 1 deletion 5.0/_navbar.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- 版本
- [4.6](4.6/)
- [5.0](5.0/)
- [5.0](5.0/)
- [6.x](6/)
3 changes: 2 additions & 1 deletion 6/_navbar.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- 版本
- [4.6](4.6/)
- [5.0](5.0/)
- [5.0](5.0/)
- [6.x](6/)
46 changes: 25 additions & 21 deletions 6/_sidebar.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
- [介绍](6/introduction)

- 概述
- [第一步](5.0/firststeps)
- [控制器](5.0/controllers)
- [提供者](5.0/providers)
- [模块](5.0/modules)
- [中间件](5.0/middlewares)
- [异常过滤器](5.0/exceptionfilters)
- [管道](5.0/pipes)
- [守卫](5.0/guards)
- [拦截器](5.0/interceptors)
- [自定义装饰器](5.0/customdecorators)
- [关于翻译](5.0/about)
- [第一步](6/firststeps)
- [控制器](6/controllers)
- [提供者](6/providers)
- [模块](6/modules)
- [中间件](6/middlewares)
- [异常过滤器](6/exceptionfilters)
- [管道](6/pipes)
- [守卫](6/guards)
- [拦截器](6/interceptors)
- [自定义装饰器](6/customdecorators)
- [关于翻译](6/about)

- [基本原理](6/fundamentals)

- [基本原理](5.0/fundamentals)
- [技术](6/techniques)

- [技术](5.0/techniques)
- [GraphQL](6/graphql)

- [GraphQL](5.0/graphql)
- [WEBSOCKETS](6/websockets)

- [WEBSOCKETS](5.0/websockets)
- [微服务](6/microservices)

- [微服务](5.0/microservices)
- [应用上下文](6/applicationcontext)

- [执行上下文](5.0/executioncontext)
- [秘籍](6/recipes)

- [秘籍](5.0/recipes)
- [CLI](6/cli)

- [CLI](5.0/cli)
- [FAQ](6/faq)

- [FAQ](5.0/faq)
- [迁移指南](6/migrationguide)

- [迁移指南](5.0/migrationguide)
- [谁在使用 Nest](6/discover)

41 changes: 41 additions & 0 deletions 6/applicationcontext.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 应用上下文

有几种安装 Nest 应用程序的方法。您可以创建一个 Web 应用程序,微服务或只是一个 Nest **执行上下文** 。 Nest 上下文是 Nest 容器的一个包装,它包含所有实例化的类。我们可以直接使用应用程序对象从任何导入的模块中获取现有实例。由于这一点,您可以充分利用 Nest 框架的优势,包括 **CRON** 任务,甚至可以在其上构建 **CLI**

为了创建一个 Nest 应用程序上下文,我们使用下面的语法:

```typescript
async function bootstrap() {
const app = await NestFactory.createApplicationContext(ApplicationModule);
// logic...
}
bootstrap();
```

之后,Nest 允许您选择在 Nest 应用程序中注册的任何实例。假设我们在 `TasksModule` 中有一个 `TasksController` 。这个类提供了一组我们想从 CRON 任务中调用可用的方法。

```typescript
const app = await NestFactory.create(ApplicationModule);
const tasksController = app.get(TasksController);
```

就是这样。要获取 `TasksController` 实例,我们必须使用 `get()` 方法。我们不必遍历整个模块树,`get()` 方法就像 **查询** 一样,自动在每个注册模块中搜索实例。但是,如果您更喜欢严格的上下文检查,则可以使用 `strict: true` 选项对象作为 `get()` 方法的第二个参数传递给它。然后,您必须通过所有模块从选定的上下文中选取特定的实例。

```typescript
const app = await NestFactory.create(ApplicationModule);
const tasksController = app.select(TasksModule).get(TasksController, { strict: true });
```

| | |
| :------------ | :----------------------------------------------------------- |
| `get()` | 检索应用程序上下文中可用的控制器或提供者(包括看守器,过滤器等)的实例。 |
| `select()` | 浏览模块树,例如,从所选模块中提取特定实例(与启用严格模式一起使用)。 |

?> 默认情况下,根模块处于选中状态。要选择任何其他模块,您需要遍历整个模块树(逐步)。

### 译者署名

| 用户名 | 头像 | 职能 | 签名 |
|---|---|---|---|
| [@zuohuadong](https://github.com/zuohuadong) | <img class="avatar-66 rm-style" src="https://wx3.sinaimg.cn/large/006fVPCvly1fmpnlt8sefj302d02s742.jpg"> | 翻译 | 专注于 caddy 和 nest,[@zuohuadong](https://github.com/zuohuadong/) at Github |
| [@Drixn](https://drixn.com/) | <img class="avatar-66 rm-style" src="https://cdn.drixn.com/img/src/avatar1.png"> | 翻译 | 专注于 nginx 和 C++,[@Drixn](https://drixn.com/) |
41 changes: 32 additions & 9 deletions 6/firststeps.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## 语言

我们爱上了 [TypeScript](https://www.tslang.cn),但最重要的是,我们喜欢 [Node.js](https://nodejs.cn/)。 这就是为什么 Nest 兼容 TypeScript 和**纯 JavaScript**。 Nest 正利用最新的语言功能,所以要使用简单的 JavaScript 框架,我们需要一个 [Babel](https://babeljs.cn) 编译器。
我们爱上了 [TypeScript](https://www.tslang.cn),但最重要的是,我们喜欢 [Node.js](https://nodejs.cn/)。 这就是为什么 Nest 兼容 TypeScript 和**纯 JavaScript**。 Nest 正利用最新的语言功能,所以要使用原生的 JavaScript 框架,我们需要一个 [Babel](https://babeljs.cn) 编译器。

在文章中,我们主要使用 TypeScript ,但是当它包含一些 Typescript 特定的表达式时,您总是可以将代码片段**切换**到 JavaScript 版本。

Expand All @@ -16,16 +16,16 @@

## 建立

使用 [Nest CLI](/5.0/cli?id=overview) 建立新项目非常简单。 只要确保你已经安装了 npm,然后在你的 OS 终端中使用以下命令:
使用 [Nest CLI](/6/cli?id=overview) 建立新项目非常简单。 只要确保你已经安装了 npm,然后在你的 OS 终端中使用以下命令:


```
$ npm i -g @nestjs/cli
$ nest new project
$ nest new project-name
```

将创建 `project` 目录, 安装node模块和一些其他样板文件,并将创建一个 `src` 目录,目录中包含几个核心文件。

`project` 目录将在 `src` 目录中包含几个核心文件。

```
src
Expand All @@ -34,13 +34,15 @@ src
└── main.ts
```

按照约定,新创建的模块应该有一个专用目录。
以下是这些核心文件的简要概述:


| | |
| ------------- |:-------------:|
| main.ts | 应用程序入口文件。它使用 `NestFactory` 用来创建 Nest 应用实例。 |
| app.module.ts | 定义 `AppModule` 应用程序的根模块。 |
| app.controller.ts | 带有单个路由的基本控制器示例。 |
| app.module.ts | 应用程序的根模块。 |
| main.ts | 应用程序入口文件。它使用 `NestFactory` 用来创建 Nest 应用实例。 |



`main.ts` 包含一个异步函数,它负责**引导**我们的应用程序:
Expand All @@ -58,12 +60,33 @@ bootstrap();
```


要创建一个 Nest 应用实例,我们使用了 `NestFactory``NestFactory` 是最重要的基础类之一,它暴露了一些静态方法用于创建应用实例。 `create()` 方法返回一个实现 `INestApplication` 接口的对象, 并提供一组可用的方法, 在后面的章节中将对此进行详细描述。
要创建一个 Nest 应用实例,我们使用了 `NestFactory` 核心类。`NestFactory` 暴露了一些静态方法用于创建应用实例。 `create()` 方法返回一个实现 `INestApplication` 接口的对象, 并提供一组可用的方法, 在后面的章节中将对此进行详细描述。 在上面的main.ts示例中,我们只是启动 HTTP 服务器,它允许应用程序等待入站 HTTP 请求。

请注意,使用 Nest CLI 搭建的项目会创建一个初始项目结构,我们鼓励开发人员将每个模块保存在自己的专用目录中。

## 平台

Nest 旨在成为一个与平台无关的框架。 通过平台,可以创建可重用的逻辑部件,开发人员可以利用这些部件来跨越多种不同类型的应用程序。 从技术上讲,Nest 可以在创建适配器后使用任何 Node HTTP 框架。 有两个支持开箱即用的 HTTP 平台:express 和 fastify。 您可以选择最适合您需求的产品。

| | |
| ------------- |:-------------:|
|platform-express|Express 是一个众所周知的 node.js 简约 Web 框架。 这是一个经过实战考验,适用于生产的库,拥有大量社区资源。 默认情况下使用 `@nestjs/platform-express` 包。 许多用户都可以使用 Express ,并且无需采取任何操作即可启用它。|
|platform-fastify| Fastify 是一个高性能,低开销的框架,专注于提供最高的效率和速度。 在[这里](6/techniques?id=性能(fastify))阅读如何使用它。|


无论使用哪种平台,它都会暴露自己的应用程序界面。 它们分别被视为 NestExpressApplication 和 NestFastifyApplication。

将类型传递给 NestFactory.create() 方法时,如下例所示,app 对象将具有专用于该特定平台的方法。 但是,请注意,除非您确实要访问底层平台API,否则无需指定类型。

```typescript
const app = await NestFactory.create<NestExpressApplication>(ApplicationModule);
```



## 运行应用程序

安装过程完成后,您可以运行以下命令启动 HTTP 服务器:
安装过程完成后,您可以在系统命令提示符下运行以下命令,以启动应用程序监听入站 HTTP 请求:

```
$ npm run start
Expand Down
1 change: 1 addition & 0 deletions 6/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 介绍
2 changes: 1 addition & 1 deletion _coverpage.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
- 交流QQ群:489719517

[GitHub](https://github.com/nestjs/nest)
[5.0](/5.0/firststeps.md)
[6.x](/6/firststeps.md)
3 changes: 2 additions & 1 deletion _navbar.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- 版本
- [4.6](4.6/)
- [5.0](5.0/)
- [5.0](5.0/)
- [6.x](6/)

0 comments on commit f440e0b

Please sign in to comment.