【发布时间】: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