简体   繁体   English

React App 部署到 Azure Web 的过程?

[英]Process for React App deployment to Azure Web?

I am currently trying to deploy the default react web app to Azure and I am encountering an issue where though I deploy the contents of my build folder to the azure hosted /site/wwwroot folder I end up on the following page when going to my hosted address: https://[project_name].azurewebsites.net/我目前正在尝试将默认的 React Web 应用程序部署到 Azure,但遇到了一个问题,尽管我将构建文件夹的内容部署到了 azure 托管的 /site/wwwroot 文件夹,但在转到托管地址: https://[project_name].azurewebsites.net/

Landing Page :登陆页面 : 在此处输入图片说明

I intend to deploy the default create-react-app react application so that I may have the process down for when I deploy my real site.我打算部署默认的 create-react-app 反应应用程序,以便我可以在部署我的真实站点时停止该过程。 The process I have followed is pretty much exactly what is mentioned in this article https://medium.com/@to_pe/deploying-create-react-app-on-microsoft-azure-c0f6686a4321我遵循的过程几乎就是本文中提到的https://medium.com/@to_pe/deploying-create-react-app-on-microsoft-azure-c0f6686a4321

  1. Create the default React App with create-react-app使用 create-react-app 创建默认的 React App
  2. Run "npm run build" to get the build folder运行“npm run build”以获取构建文件夹
  3. Go into the Azure React Portal and create a new Web App ***进入 Azure React 门户并创建一个新的 Web 应用程序 ***
  4. FTP / Git deploy the contents of the local build folder into the Azure website's /site/wwwroot/ folder FTP/Git 将本地build文件夹的内容部署到Azure网站的/site/wwwroot/文件夹中
  5. For overkill I added the below web.config file to handle future routes, but have also tried without this step为了矫枉过正,我添加了下面的 web.config 文件来处理未来的路由,但也尝试过没有这一步

In the end my Azure site's contents look like this最后我的 Azure 站点的内容是这样的

Folder contents :文件夹内容: 在此处输入图片说明

At this point when I try to access the Azure site I get the "Hey, Node developers!"此时,当我尝试访问 Azure 站点时,我收到“嘿,Node 开发人员!” page which implies my code is not deployed.这意味着我的代码未部署的页面。 Any thoughts as to why this might be the case?关于为什么会这样的任何想法?

*** I have a hunch that during the configuring of the Azure Web Api something is not set up correctly perhaps because I select Node 10.14 as my Runtime stack simply because that is the version of Node that I have installed and am using with my local React app. *** 我有一种预感,在 Azure Web Api 的配置过程中,有些东西没有正确设置,这可能是因为我选择 Node 10.14 作为我的运行时堆栈,仅仅是因为这是我安装的 Node 版本,并与我的本地一起使用反应应用程序。

Thank you folks for your time.谢谢你们的时间。

Another approach is to configure Azure Linux Web App Service to run a startup command to serve your React app in "Settings > General settings > Startup Command" :另一种方法是配置Azure Linux Web 应用服务以在“设置>常规设置>启动命令”中运行启动命令来为您的 React 应用程序提供服务:

pm2 serve /home/site/wwwroot/ --no-daemon

Remember to change the path to your build path (The path to your index.html file).请记住更改构建路径的路径(您的 index.html 文件的路径)。

If you use react-router and wants to make any direct access on custom routes be handled by index.html you need to add --spa option on the same command.如果您使用 react-router 并希望通过 index.html 处理对自定义路由的任何直接访问,则需要在同一命令上添加--spa选项。

pm2 serve /home/site/wwwroot/ --no-daemon --spa

Using --spa option pm2 will automatically redirect all queries to the index.html and then react router will do its magic.使用--spa选项--spa将自动将所有查询重定向到 index.html,然后反应路由器将发挥其魔力。

You can find more information about it in pm2 documentation: https://pm2.keymetrics.io/docs/usage/pm2-doc-single-page/#serving-spa-redirect-all-to-indexhtml您可以在 pm2 文档中找到有关它的更多信息: https ://pm2.keymetrics.io/docs/usage/pm2-doc-single-page/#serving-spa-redirect-all-to-indexhtml

I've coped with the same problem recently: React router direct links not working on Azure Web App Linux我最近解决了同样的问题: React router direct links not working on Azure Web App Linux

You have created a Linux App Service - your web.config won't work because there is no IIS.您已创建 Linux 应用服务 - 您的 web.config 将无法工作,因为没有 IIS。

If you don't select node as the runtime stack, your app will work for the most part because it serves the files like a static web host.如果您不选择 node 作为运行时堆栈,您的应用程序将在大多数情况下工作,因为它像静态 Web 主机一样提供文件。 However I would suggest to keep the runtime stack as node and add the following file to your deployment in the wwwroot folder:但是,我建议将运行时堆栈保留为节点,并将以下文件添加到wwwroot文件夹中的部署中:

ecosystem.config.js

module.exports = {
    apps: [
        {
            script: "npx serve -s"
        }
    ]
};

https://burkeknowswords.com/this-is-how-to-easily-deploy-a-static-site-to-azure-96c77f0301ff https://burkeknowswords.com/this-is-how-to-easily-deploy-a-static-site-to-azure-96c77f0301ff

There's an extremely simple way to overcome this problem, and although it is not perfect, it works both on AWS, Microsoft Azure, and probably any other hosting:有一种非常简单的方法可以解决这个问题,虽然它并不完美,但它适用于 AWS、Microsoft Azure 和可能的任何其他托管:

  • Just point the error document to: index.html只需将错误文档指向:index.html

I found it out here: https://stackoverflow.com/a/52343542/3231884我在这里找到了: https : //stackoverflow.com/a/52343542/3231884

Disclaimer : This solution is not perfect and impacts SEO.免责声明:此解决方案并不完美,会影响 SEO。 Google doesn't rank well sites that throw 404s.谷歌不会对抛出 404 的网站进行排名。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM