使用 Loopback 4 调用现有 API [英] Call an existing API using Loopback 4

查看:48
本文介绍了使用 Loopback 4 调用现有 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个预先开发的 API 连接到我的后端.我使用环回 4 作为我的反应应用程序的数据提供者.我想使用 Loopback 调用 API 并在我的应用程序中获取结果.我浏览了环回 4 文档 调用其他 API 但发现它有点混乱.我的 API 返回的数据是:
{ "name": "xyz", "place": "xyz", "email": "xyz";}

I have a pre-developed API connected to my backend. I am using loopback 4 as a data provider for my react application. I want to call the API using Loopback and get the result in my application. I went through the loopback 4 documentation calling other API's but found it a bit confusing. Data returned by my API is :
{ "name": "xyz", "place": "xyz", "email": "xyz" }

我希望使用 Loopback 将此数据返回到我的应用程序.我不想从我的 React 应用程序直接调用 API.我希望 Loopback 进行所有 API 调用.
非常感谢任何帮助.

I want this data to be returned to my application using Loopback. I do not want to make a direct API call from my React application. I want Loopback to make all the API calls.
Any help is much appreciated.

推荐答案

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.

这篇关于使用 Loopback 4 调用现有 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆