React应用程序路由部署到IIS后不起作用

Dar*_*Lau 4 iis reactjs

我是 Reactjs 新手,目前在 React 应用程序的 IIS 部署方面面临一些问题。

我执行 npm run build 并生成一个构建文件夹,然后我将此文件夹复制到我的 IIS。当我浏览页面时,我能够查看空白页面,但是当我导航到测试路线时,它显示 404。

我尝试添加“主页”:“/”或“主页”:“。” 在我的 package.json 中但仍然相同。

索引.html

在此输入图像描述

这是我的构建结构

在此输入图像描述

任何帮助表示赞赏。

小智 6

每当您将反应页面部署到生产环境时,您都会遇到这个问题。请参考此页面:https://inthetechpit.com/2019/06/13/handle-client-side-routes-with-iis-on-page-refresh-react-app/

该问题发生在服务器中(不仅仅是 IIS,而是所有服务器)。您将需要安装扩展,然后将配置文件添加到前端构建目录的路径中。

要安装的扩展位于此处:https://www.iis.net/downloads/microsoft/url-rewrite

应放置在前端构建目录中的 web.config 文件的内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
    <rewrite>
      <rules>
        <rule name="ReactRouter Routes" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          </conditions>
          <action type="Rewrite" url="index.html" />
        </rule>

      </rules>
    </rewrite>
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)