问题描述
如何在IIS Web服务器中托管reactjs
应用程序?
我使用npm run build
并将文件替换到服务器中,并创建引用这些文件的新站点。
推荐答案
运行下面的命令后,输出将在项目中创建一个名为"Build"的新文件夹,其中包含生产版本。我们可以通过将这些文件复制到IIS网站的根目录来托管项目,网站根文件夹应该能够被匿名帐户正确访问,因此如果该文件夹不是默认网站,我们应该授予IUSR帐户对该文件夹的完全访问权限。 在IIS绑定模块中添加网站绑定后 我们可以使用下面的地址正确访问该网站。
http://localhost
此外,如果我们在Reaction应用程序中添加某些路由功能(多个组件),则必须安装IIS URL重写扩展。 https://www.iis.net/downloads/microsoft/url-rewrite 成功安装后,我们需要在IIS网站的根目录下创建一个web.config文件,其中包含以下内容。
<?xml version="1.0"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="React 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>
</system.webServer>
</configuration>
如果问题仍然存在,请随时通知我。