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

时间:2017-03-13 20:54:31

标签: node.js reactjs iis iis-10

在同一问题上观察了几个主题。没有人为我工作。 事情是: 我有从create-react-app设计的ReactJS + Redux应用程序。实际上它可以工作但是当app冲浪以不同于root的URL开始时,会抛出404。 已安装iisnode并尝试了网络中推荐的不同重写规则,但没有任何效果。 最后是这一个:       

    <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>

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

编辑:另一个奇怪的事情是iisnode可以使用js文件处理重定向,但我的应用程序在index.html中呈现,当我尝试将index.html指向处理程序时,它实际上向我展示了空白页。

1 个答案:

答案 0 :(得分:4)

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>
相关问题