【问题标题】:Why web.config is needed to deploy the react application in IIS为什么需要 web.config 在 IIS 中部署 React 应用程序
【发布时间】:2019-05-08 05:24:20
【问题描述】:

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

<?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 应用程序吗?

【问题讨论】:

  • 您需要重写任何其他服务器的 URL,而不仅仅是 IIS。这是因为 SPA 如何处理路由。基本上它会发回 SPA 加载到的 html shell。
  • @penleychan 用于 Angular js 应用程序,我没有添加任何重写 URL 来在 server.angular 中运行它。 Angular 也是 SPA。
  • 嗯,很确定你需要它来用于 angularjs,我记得为它设置了一个,否则如果你刷新你会得到 404。

标签: javascript reactjs typescript react-router


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-10
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    • 2014-04-18
    相关资源
    最近更新 更多