jackdi 发表于 2023-3-27 23:20

请问如何在AWS上运行JSON Server

写了一个简单的react app。
调用了两个json文件。本地运行没问题(数据很少,也不是啥重要的数据,所以不考虑安全性问题)。
部署到aws ec2的 windows iis上面,react页面可以显示,但无法调用json数据。

浏览器里的错误如下:
Access to fetch at 'http://localhost:3000/abc' from origin 'http://*.*.*.*>' has been blocked by CORS policy: The request client is not a secure context and the resource is in more-private address space `local`.

http://localhost:3000/abc 可以正常显示数据。

在chagpt里面问了半天,各种方法都试了,还是不行。
主要是不太懂服务器。也不太清楚到底是哪里出了问题。

有大牛能指点一下,给个思路。
两天了都没搞定,都有点想放弃了。

angest 发表于 2023-3-28 12:13

把你的json放到和react app同域的服务器上

jackdi 发表于 2023-3-28 12:22

angest 发表于 2023-3-28 12:13
把你的json放到和react app同域的服务器上

json文件和react文件在同一文件夹中。
代码中的url使用的是http://localhost:3000/abc

当使用http://localhost访问是,可以链接到数据库
当使用public ip访问时,可以看到react页面,但不能访问json数据库

Mushroomm 发表于 2023-3-28 12:28

http://localhost:3000/abc是指browser所在机器。
你确定让每个用户都在他的本地host一个server? :lol

pengruijun 发表于 2023-3-28 12:33

用relative path to access json file. For example, ./abc

jackdi 发表于 2023-3-28 13:08

Mushroomm 发表于 2023-3-28 12:28
http://localhost:3000/abc是指browser所在机器。
你确定让每个用户都在他的本地host一个server?...

不明白你的意思
在react中,useFetch需要使用什么url呢?

const { data, isPending, error } = useFetch('http://localhost:3000/abc')

我以为是public ip会自动指向这个localhost呢

jackdi 发表于 2023-3-28 13:12

pengruijun 发表于 2023-3-28 12:33
用relative path to access json file. For example, ./abc

useFetch可以使用相对地址吗?

const { data, isPending, error } = useFetch('http://localhost:3000/abc')

kr2000 发表于 2023-3-28 13:18

你得先找到json file服务器端的public URL,然后放到fetch里
http://localhost:3000/abc只是本地地址

建议你把json文件放s3,然后开放public access. 这样本地和服务器都访问云端的json文件

Mushroomm 发表于 2023-3-28 13:31

jackdi 发表于 2023-3-28 13:08
不明白你的意思
在react中,useFetch需要使用什么url呢?



哪个server host json data, 就用哪个server的domain name/ip address.

NinaDavid 发表于 2023-3-28 21:50

IIS烦,需要装IISNODE 和 URLREWRITE

阿盛 发表于 2023-6-26 11:17

const { data, isPending, error } = useFetch('/abc')

collect2013 发表于 2023-6-27 02:04

直接放在s3, bucket setting 就可也设置Cors, Static hosting直接上s3好搞很多。
页: [1]
查看完整版本: 请问如何在AWS上运行JSON Server

Advertisement
Advertisement