[英]How to use fetch to fetch data from AWS RDS in a react application?
I'm learning Node + React.我正在学习 Node + React。 In my Node + React application, I have the following component
FetchData.js
to fetch data from localhost
:在我的 Node + React 应用程序中,我有以下组件
FetchData.js
从localhost
获取数据:
import React from 'react';
class FetchData extends React.Component {
static async getData() {
let response = await fetch('http://localhost:4000/my-project');
let body = await response.json();
return body
}
}
export default FetchData;
I could see the json objects by typing localhost:4000
in the browser.我可以通过在浏览器中键入
localhost:4000
来查看 json 对象。
Now I'm hosting the data in AWS RDS.现在我在 AWS RDS 中托管数据。 I have an endpoint like this:
我有一个这样的端点:
my-project.abc12345def.eu-west-2.rds.amazonaws.com:3306
How can I fetch data from the endpoint like what I did in FetchData.js
?如何像我在
FetchData.js
中所做的那样从端点获取数据? I tried to replace localhost
with the endpoint url, no luck;我尝试用端点 url 替换
localhost
,但没有成功; tried to type the endpoint url in a browser, could not see its content.试图在浏览器中键入端点 url,但看不到其内容。 I suppose I need to be able to see its content to be able to fetch data?
我想我需要能够看到它的内容才能获取数据?
I will give you most simple difference我会给你最简单的区别
Fetch is a way to access HTTP resources like http://localhost:4000/my-project
. Fetch 是一种访问 HTTP 资源的方法,例如
http://localhost:4000/my-project
。
Your RDS endpoint (Most probably MYSQL) does not run an HTTP server.您的 RDS 端点(很可能是 MYSQL)不运行 HTTP 服务器。
So these endpoints (Links) are different, and they are not for the same purpose.所以这些端点(链接)是不同的,它们不是为了同一个目的。
Edit: You could connect to RDS using a MYSQL client from your server running on http://localhost:4000
which then return the data to fetch request.编辑:您可以使用 MYSQL 客户端从运行在
http://localhost:4000
上的服务器连接到 RDS,然后返回数据以获取请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.