简体   繁体   English

如果我部署我的 React 应用程序,指向 localhost 的链接是否仍然有效,还是我还需要托管 localhost 应用程序?

[英]If I deploy my react app, will the link to a localhost still be valid, or will I also need to host the localhost app?

Basically the heading.基本上是标题。 I have a strapi app at localhost:1337 which I will fetch in React.我在 localhost:1337 有一个 Strapi 应用程序,我将在 React 中获取它。 I'm not very sure how localhost works, and therefore I want to know if the path will still be relevant when I deploy the react app.我不太确定 localhost 是如何工作的,因此我想知道在部署 React 应用程序时该路径是否仍然相关。

When you deploy your react.js app on any server your url named http://localhost:1337/Dashboard will be changed.当您在任何服务器上部署 react.js 应用程序时,名为 http://localhost:1337/Dashboard 的 url 将被更改。 In it http://localhost:1337/ is the base url or domain name.其中 http://localhost:1337/ 是基本 URL 或域名。 Which will change the server to the new one.这会将服务器更改为新服务器。

your code will maintain same value for that API and you will have to re-build your code each time you change your API, (most of people use low cost hosting provider which allow only port 80 to be used) my advice is to move your endpoit (backend url) outside your code in a json, .env file ... but what will work on most of platform is a variable defined in your public/index.html (not a best pratice but it will work) ex:您的代码将为该 API 保持相同的价值,每次更改 API 时,您都必须重新构建代码,(大多数人使用低成本的托管服务提供商,只允许使用端口 80)我的建议是移动您的在 json、.env 文件中的代码之外的端点(后端 url)……但是在大多数平台上可以使用的是 public/index.html 中定义的变量(不是最佳实践,但可以使用)例如:

  <html>
    <head>
    <!-- you will add this tag here it will contain your backend url -->
    <script> 
      var bakendUrl = "http://....";
    </script> 
    <!-- some other code here -->
    </head> 

    <body>
      <div id="root"></div>
    </body>
   </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让我的 React 应用程序在 localhost:3000 中编译 - How can I get my react app to compile in localhost:3000 为什么我的 React App 没有在 localhost 上加载? - Why is my React App not loading on localhost? 我的 React 应用程序在本地主机上显示空白页面 - My react app is showing a blank page on localhost Containerzied React 应用程序和 NGINX 不在我想要的本地主机端口上提供服务 - Containerzied React app and NGINX not serving on the localhost port I want 在HOST中不使用&#39;localhost&#39;进行create-react-app开发 - Not using 'localhost' in HOST for development with create-react-app 我尝试部署React Heroku应用时出现无效的主机头错误 - Invalid host header error as I try to deploy a react heroku app 我的应用程序将 localhost:3000 作为服务器。 但我有 localhost:4000 作为服务器 - my app is taking localhost:3000 as the server. but i have localhost:4000 as the server 我可以在常规主机上部署我的 ReactJS 应用程序吗? - Can I deploy my ReactJS app on a regular host? 错误:不变失败:你不应该使用<Link>外面<Router>当我尝试使用 localhost 测试我的应用程序时 - Error: Invariant failed: You should not use <Link> outside a <Router> when I try to test my app using localhost 我的 pero 中是否需要 gh-pages 文件夹来托管 React 应用程序? - Do I need gh-pages folder in my pero to host react app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM