如何将React App Localhost更改为IP?3000 IP?

发布于 2025-01-21 03:29:06 字数 162 浏览 1 评论 0原文

我正在使用付费API。他们将我的IP定为白名。我可以从Thunder Client和Postman应用中获取数据。但是,当我要从React应用程序中获取数据时。没什么做任何事情。基本上是本地主机:3000在不同的IP上托管吗?因此,如何从IP(103.124.251.85)中托管我的React-App本地主机

I am working with a paid API. They whitelisted my ip. I can fetch there data from Thunder Client and postman app. But when I'm going to fetch the data from my react app. It's nothing do anything. Basically localhost:3000 are hosted on different IP right? So how to host my react-app local host from my IP(103.124.251.85)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挖鼻大婶 2025-01-28 03:29:06

要更改 React 应用程序的主机,您只需在环境中添加主机,如下所示:

HOST=you_system_ip

或者您也可以在包 json 文件中添加脚本,如下所示:

    "start": "HOST=you_system_ip react-scripts start",

To change your host for react app you just have to add HOST in your env like below:

HOST=you_system_ip

Or you can also add script in your package json file as well like below:

    "start": "HOST=you_system_ip react-scripts start",
〃温暖了心ぐ 2025-01-28 03:29:06

在 package.json 中,您可以将“start”脚本更改为

"start": "SET HOST=103.124.251.85 && react-scripts start"

或者,您可以在项目的根目录中创建一个文件 .env 并设置:

HOST=103.124.251.85

如果您需要通过 HTTPS 运行应用程序,请执行以下操作:

  • 在包中在 .json 中添加预启动脚本:它将私钥和 SSL 证书的 crt 组合成 pem,然后将其复制到 webpack 服务器模块中。
"scripts": {
    "prestart": "type dev_certs\\cert.key dev_certs\\cert.crt > dev_certs\\server.pem && copy /y dev_certs\\server.pem node_modules\\webpack-dev-server\\ssl",
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  }
  • 在您设置的 .env 文件中:
PORT=443
HOST=103.124.251.85
HTTPS=true

In your package.json you can change the "start" script to

"start": "SET HOST=103.124.251.85 && react-scripts start"

Or, you can create a file .env in the root of your project and set:

HOST=103.124.251.85

If you need to run you app over HTTPS, here is what to do:

  • In package.json you add the prestart script: it combines the private key and the crt of your SSL certificate into a pem and then it will copy it in the webpack server module.
"scripts": {
    "prestart": "type dev_certs\\cert.key dev_certs\\cert.crt > dev_certs\\server.pem && copy /y dev_certs\\server.pem node_modules\\webpack-dev-server\\ssl",
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  }
  • In the .env file you set:
PORT=443
HOST=103.124.251.85
HTTPS=true
不必了 2025-01-28 03:29:06

如果您遇到错误“allowedHosts”为空:

options.allowedHosts[0] should be a non-empty string.

您可以通过传递以下环境变量来禁用此检查:

DANGEROUSLY_DISABLE_HOST_CHECK=true HOST=xxxx npm run start

If you're hitting an error 'allowedHosts' is empty:

options.allowedHosts[0] should be a non-empty string.

You can disable this check by passing the following env variable:

DANGEROUSLY_DISABLE_HOST_CHECK=true HOST=xxxx npm run start
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文