Skip to content

Commit 6355de9

Browse files
author
huadong zuo
authored
Merge pull request #388 from GuoJikun/patch-1
Update recipes.md
2 parents 0a82279 + 8f326fd commit 6355de9

File tree

1 file changed

+135
-2
lines changed

1 file changed

+135
-2
lines changed

10/recipes.md

Lines changed: 135 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,140 @@
22

33
## REPL
44

5-
(待翻译)
5+
REPL 是一个简单的交互式环境,它接受单个用户输入,执行这些输入,并将结果返回给用户。REPL 功能允许您直接从终端检查依赖关系图并调用提供程序(和控制器)上的方法。
6+
7+
### 用法
8+
9+
若要在 REPL 模式下运行 NestJS 应用程序,请创建一个新 `repl.ts` 文件 (与现有的 `main.ts` 文件放在一起) ,并在其中添加以下代码:
10+
11+
```typescript
12+
@@filename(repl)
13+
import { repl } from '@nestjs/core';
14+
import { AppModule } from './app.module';
15+
16+
async function bootstrap() {
17+
await repl(AppModule);
18+
}
19+
bootstrap();
20+
@@switch
21+
import { repl } from '@nestjs/core';
22+
import { AppModule } from './app.module';
23+
24+
async function bootstrap() {
25+
await repl(AppModule);
26+
}
27+
bootstrap();
28+
```
29+
30+
现在可以在终端中,使用以下命令启动 REPL:
31+
32+
```bash
33+
$ npm run start -- --entryFile repl
34+
```
35+
36+
> info **提示** `repl` 返回 [Node.js REPL server](https://nodejs.org/api/repl.html) 对象.
37+
38+
启动并运行后,您应该会在控制台中看到以下消息:
39+
40+
```bash
41+
LOG [NestFactory] Starting Nest application...
42+
LOG [InstanceLoader] AppModule dependencies initialized
43+
LOG REPL initialized
44+
```
45+
46+
现在你可以开始与依赖图交互了。例如,您可以检索 `AppService` (我们在这里使用 starter 项目作为示例)并调用 `getHello()` 方法:
47+
48+
```typescript
49+
> get(AppService).getHello()
50+
'Hello World!'
51+
```
52+
53+
您可以从终端中执行任何 JavaScript 代码,例如,将 AppController 的 实例分配给局部变量,并使用 `await` 调用异步方法:
54+
55+
```typescript
56+
> appController = get(AppController)
57+
AppController { appService: AppService {} }
58+
> await appController.getHello()
59+
'Hello World!'
60+
```
61+
62+
要显示给定 provider 或 controller 中可用的所有公共方法,请使用 `methods()` 函数,如下所示:
63+
64+
```typescript
65+
> methods(AppController)
66+
67+
Methods:
68+
getHello
69+
```
70+
71+
To print all registered modules as a list together with their controllers and providers, use `debug()`.
72+
要将所有注册的模块以及它们的控制器和提供者打印为一个列表,请使用 `debug()`
73+
74+
```typescript
75+
> debug()
76+
77+
AppModule:
78+
- controllers:
79+
AppController
80+
- providers:
81+
AppService
82+
```
83+
84+
快速演示:
85+
86+
<figure><img src="/assets/repl.gif" alt="REPL example" /></figure>
87+
88+
您可以在下面的小节中找到有关现有的、预定义的本机方法的更多信息。
89+
90+
### 原生函数
91+
92+
内置的 NestJS REPL 提供了一些全局可用的原生函数。你可以调用 `help()` 来列出它们。
93+
94+
If you don't recall what's the signature (ie: expected parameters and a return type) of a function, you can call `<function_name>.help`.
95+
For instance:
96+
97+
如果你不记得函数的签名是什么(即:预期参数和返回类型),你可以调用 `<function_name>.help`
98+
例如
99+
100+
```text
101+
> $.help
102+
Retrieves an instance of either injectable or controller, otherwise, throws exception.
103+
Interface: $(token: InjectionToken) => any
104+
```
105+
106+
> info **提示** 这些函数接口使用[TypeScript函数类型表达式语法](https://www.typescriptlang.org/docs/handbook/2/functions.html#function-type-expressions)编写。
107+
108+
| Function | Description | Signature |
109+
| ------------ | --------------------------------------------------------- | --------------------------------------------------------------------- |
110+
| `debug` | 将所有注册的模块以及它们的控制器和提供者打印为一个列表。 | `debug(moduleCls?: ClassRef \| string) => void` |
111+
| `get` or `$` | 获取可注入对象或控制器的实例,否则抛出异常。 | `get(token: InjectionToken) => any` |
112+
| `methods` | 显示给定提供程序或控制器上可用的所有公共方法。 | `methods(token: ClassRef \| string) => void` |
113+
| `resolve` | 解析可注入对象或控制器的瞬态或请求作用域实例,否则抛出异常。 | `resolve(token: InjectionToken, contextId: any) => Promise<any>` |
114+
| `select` | 允许在模块树中导航,例如,从选定的模块中拉出特定实例。 | `select(token: DynamicModule \| ClassRef) => INestApplicationContext` |
115+
116+
### 监视模式
117+
118+
在开发过程中,可以在监视模式下运行REPL,以自动反映所有代码更改:
119+
120+
```bash
121+
$ npm run start -- --watch --entryFile repl
122+
```
123+
124+
这有一个缺陷,REPL的命令历史记录在每次重新加载后都会被丢弃,这可能很麻烦。
125+
幸运的是,有一个非常简单的解决方案。像这样修改你的 `bootstrap` 函数:
126+
127+
```typescript
128+
async function bootstrap() {
129+
const replServer = await repl(AppModule);
130+
replServer.setupHistory(".nestjs_repl_history", (err) => {
131+
if (err) {
132+
console.error(err);
133+
}
134+
});
135+
}
136+
```
137+
138+
现在,在运行/重新加载之间保存历史记录。
6139

7140
## CRUD生成器
8141

@@ -3512,4 +3645,4 @@ export class AppModule {}
35123645

35133646
| 用户名 | 头像 | 职能 | 签名 |
35143647
|---|---|---|---|
3515-
| [@ThisIsLoui](https://github.com/ThisIsLoui) | <img class="avatar-66 rm-style" height="70" src="https://avatars.githubusercontent.com/u/69883404?s=96&v=4"> | 翻译 | 你好,这里是 Loui |
3648+
| [@ThisIsLoui](https://github.com/ThisIsLoui) | <img class="avatar-66 rm-style" height="70" src="https://avatars.githubusercontent.com/u/69883404?s=96&v=4"> | 翻译 | 你好,这里是 Loui |

0 commit comments

Comments
 (0)