[英]How do i fetch data from api endpoint
I'm running nextjs for my front end (port 3000), trying to fetch api endpoints from strapi (port 1337).我正在为我的前端(端口 3000)运行 nextjs,试图从 Strapi(端口 1337)获取 api 端点。 How do I go about using my frontend application in order to fetch data from my backend.
我如何使用我的前端应用程序来从我的后端获取数据。 Is there a way that both ports can run simultaneously, or am I doing something wrong?
有没有办法让两个端口同时运行,或者我做错了什么?
If I run on port 3000 I receive a GET request error ERR_CONNETION_REFUSED, obviously - because the data on port 1337 isn't available because port 1337 isn't running.如果我在端口 3000 上运行,我会收到一个 GET 请求错误 ERR_CONNETION_REFUSED,显然 - 因为端口 1337 上的数据不可用,因为端口 1337 没有运行。
My file structure for the project is the following:我的项目文件结构如下:
my_app我的应用程序
--backend folder(strapi files and runs on on port 1337) --backend 文件夹(strapi 文件并在端口 1337 上运行)
--frontend folder(contains all nextjs and runs on port 3000) --frontend 文件夹(包含所有 nextjs 并在端口 3000 上运行)
Code :代码:
import Header from '../components/Header'
import useFetch from '../public/hooks/useFetch'
export default function Home() {
const { loading, error, data } = useFetch('http://localhost:1337/reviews')
return (
<>
<Header/>
HOME
</>
)
}
To make your life easier, I do recommend you to install "Documentation" on the marketplace.为了让您的生活更轻松,我建议您在市场上安装“文档”。 After that, you will have access to "Documentation in plugins", there you will be able to check your API, and use "Retrieve your JWT token" to obtain a test token to test all your API.
之后,您将可以访问“插件中的文档”,在那里您将能够检查您的 API,并使用“检索您的 JWT 令牌”来获取测试令牌以测试您的所有 API。
From there you will have access to Swagger App, and you will be able to check all the API functionality.从那里您将可以访问 Swagger 应用程序,并且您将能够检查所有 API 功能。
Now, you can access all your API from the server URL ie: GET http://localhost:1337/helpers
现在,您可以从服务器 URL 访问所有 API,即:
GET http://localhost:1337/helpers
Note: Be sure to make your APIs accessible to your roles on Settings/Roles on the Admin panel.注意:请务必让您的 API 可供您在管理面板上的设置/角色上访问。 From there, you can also check the URI you will be using.
从那里,您还可以检查将使用的 URI。
In your case, to access http://localhost:1337/reviews
be sure to make reviews accessible on the public or private side respectably.在您的情况下,要访问
http://localhost:1337/reviews
确保在公共或私人方面可访问评论。
After that, you will access your strapi endpoints from the strapi URL as //localhost:1337/your_endpoint之后,您将从trapi URL访问您的trapi端点//localhost:1337/your_endpoint
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.