为什么需要web.config来在IIS中部署react应用程序

问题描述 投票:0回答:1

我是一个反应初学者。我们创建了一个小型反应应用程序,需要部署在服务器(IIS)上。经过谷歌搜索后,我知道需要将 web.config 添加到 React 项目中,它将负责 URL 路由。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Static Assets" stopProcessing="true">
          <match url="([\S]+[.](html|htm|svg|js|css|png|gif|jpg|jpeg))" />
          <action type="Rewrite" url="/{R:1}"/>
        </rule>
        <rule name="ReactRouter Routes" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/index.html" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

为什么我们需要在 web.config 中添加重写规则来在 IIS 中部署 React 应用程序。我可以在没有

web.config
的情况下运行 React 应用程序吗?

javascript reactjs typescript react-router
1个回答
0
投票

服务器需要拥有您在项目中配置的 URL 路由信息,如果没有这些信息,当您刷新或重新加载网站上的内部页面时,客户端将向服务器请求这些页面,但由于它们不那里不存在你会得到一个错误。将路由信息提供给服务器将有助于检索正确的页面并将其返回给客户端。

© www.soinside.com 2019 - 2024. All rights reserved.