且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 Loopback 4 调用现有 API

更新时间:2023-02-15 16:02:28

LoopBack 4 应用程序仍然是 Node.js 应用程序.因此,您可以通过内置的 http 模块直接在 Controller 中发出请求,或者通过导入另一个包(例如 request).

A LoopBack 4 application is still a Node.js application. Hence, you can make the request directly in the Controller via the built-in http module, or by importing another package (e.g. request).

从那里,结果可以被改变(如果需要)然后在控制器中返回.

From there, the result can be mutated (if needed) then returned within the Controller.

如果 API 在众多控制器中通用,那么建议重构服务背后的请求.服务受益于在运行时通过 this.boot() 启动,并使用 @inject@service 注入控制器.

If API is commonly-used across numerous Controllers, then it is recommended to refactor the requests behind a Service. Services benefit from being bootable at runtime via this.boot() and injectable into controllers using @inject or @service.