小编典典

如何在IIS上正确设置ReactJS应用?

reactjs

看了几个关于同一问题的话题。没有人为我工作。事情是:我有从create-react-app设计的ReactJS +
Redux应用程序。实际上,它可以工作,但是当应用程序浏览以不同于root的URL开头时,抛出404。安装iisnode并尝试了Web中推荐的不同重写规则,但没有任何效果。最后一个是:

    <rule name="Redirect HTTP to HTTPS">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent"/>
    </rule> -->

    <rule name="Redirect non-existent paths to root">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_URI}" pattern="^/initialLogin/*" ignoreCase="true" negate="true" />            
      </conditions>
      <action type="Redirect" url="/" appendQueryString="false" />
    </rule>
  </rules>
</rewrite>

我唯一需要做的就是通过根url发送所有传入请求,并将其余的地址传递给它,以便SPA可以正常工作。托管:AzureVM IIS:v10

编辑:另一个奇怪的是,它们iisnode可以处理js文件的重定向,但是我的应用程序会渲染index.html,当我尝试将其指向index.html处理程序时,实际上显示了一个空白页。


阅读 389

收藏
2020-07-22

共1个答案

小编典典

web.config对我有用。希望它能帮助别人。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <!-- This part is responsible for URL rewrites -->
        <rewrite>
          <rules>
            <rule name="ReactJS Routes" stopProcessing="true">
              <match url=".*" />
              <conditions logicalGrouping="MatchAll">
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
              </conditions>
              <action type="Rewrite" url="/" />
            </rule>
          </rules>
        </rewrite>
        <!-- This allows CORS for testing purposes -->
        <httpProtocol>
         <customHeaders>
           <add name="Access-Control-Allow-Origin" value="*" />
         </customHeaders>
       </httpProtocol>
  </system.webServer>
</configuration>
2020-07-22