Read-Eval-Print-Loop (REPL)¶
REPL 是一个简单的交互式环境,可将单个用户输入,执行它们并将结果返回给用户。 REPL 功能使您可以直接从终端检查提供商(和控制器)上的依赖关系图并调用方法。
用法¶
要以模式运行您的 Nestjs 应用程序,请创建一个新的repl.ts
文件(与main.ts
同目录),然后内部添加以下代码:
现在,在您的终端中,使用以下命令启动重复:
repl
返回一个node.js REPL 服务器对象。
启动并运行后,您应该在控制台中看到以下消息:
现在,您可以开始与依赖关系图进行交互。
例如,你可以检索一个AppService
(我们在这里使用 starter 项目作为示例)并调用getHello()
方法:
你可以在你的终端中执行任何 JavaScript 代码,例如,将AppController
的实例分配给一个局部变量,并使用await
调用一个异步方法:
要显示给定提供者或控制器上可用的所有公共方法,使用methods()
函数,如下所示:
使用debug()
将所有已注册的模块连同它们的控制器和提供者以列表的形式打印出来。
Quick demo:
您可以在下一节中找到有关现有的预定义本机方法的更多信息。
本机函数¶
The built-in NestJS REPL comes with a few native functions that are globally available when you start REPL. You can call help()
to list them out.
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
.
For instance:
info Hint Those function interfaces are written in TypeScript function type expression syntax.
Function | Description | Signature |
---|---|---|
debug |
Print all registered modules as a list together with their controllers and providers. | debug(moduleCls?: ClassRef \| string) => void |
get or $ |
Retrieves an instance of either injectable or controller, otherwise, throws exception. | get(token: InjectionToken) => any |
methods |
Display all public methods available on a given provider or controller. | methods(token: ClassRef \| string) => void |
resolve |
Resolves transient or request-scoped instance of either injectable or controller, otherwise, throws exception. | resolve(token: InjectionToken, contextId: any) => Promise<any> |
select |
Allows navigating through the modules tree, for example, to pull out a specific instance from the selected module. | select(token: DynamicModule \| ClassRef) => INestApplicationContext |
观看模式¶
During development it is useful to run REPL in a watch mode to reflect all the code changes automatically:
This has one flaw, the REPL's command history is discarded after each reload which might be cumbersome.
Fortunately, there is a very simple solution. Modify your bootstrap
function like this:
Now the history is preserved between the runs/reloads.