Node.js 为什么在将Express与React.js一起使用时,静态fies会得到401?
设置:Node.js 为什么在将Express与React.js一起使用时,静态fies会得到401?,node.js,reactjs,iis,reverse-proxy,Node.js,Reactjs,Iis,Reverse Proxy,设置: 带有express的Node.js服务器在localhost:3000(具有到active directory的ldap会话)上运行,并将SSO与包Node expose sspi一起使用 IIS反向代理在localhost:80上运行,并重定向到节点服务器localhost:3000 在开发过程中,端口localhost:3001上的React Dev服务器在package.json 问题: 我有一个node express服务器,为我的react应用程序中的生成文件夹提供服务:
- 带有express的Node.js服务器在localhost:3000(具有到active directory的ldap会话)上运行,并将SSO与包
Node expose sspi一起使用
- IIS反向代理在localhost:80上运行,并重定向到节点服务器localhost:3000
- 在开发过程中,端口localhost:3001上的React Dev服务器在
package.json
app.use(express.static(path.join(__dirname, "client", "build")));
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "client", "build", "index.html"));
});
react应用程序本身只是一个简单的hello world示例。但当我通过反向代理进入服务器时:http://myservername 页面将打开,但我在控制台中看到此错误:
得到http://myserver/static/js/2.d0df15ed.chunk.js net::ERR_中止401(未经授权)
页面将被加载
我有一个名为/api/me
的测试路径。此路由仅从SSO(单点登录)获取用户,并将其作为json返回
现在我有3种不同的场景:
- 客户端只是通过简单的获取请求调用这个api,然后说Hello{username}==>它只是显示“Hello”和静态文件的401:当我在浏览器中直接打开静态文件的路径时,它就工作了强>
- 当我直接在浏览器中打开路由时,我得到了我期望的json
- 当我在react-dev服务器(localhost:3001)上打开路由时,我会得到“Hello请确保网站的根目录已被授予对
帐户的完全访问权限。”IUSR
此外,请尝试在项目的根目录中配置
扩展。iisurl Rewrite
添加包含以下内容的
文件webconfig
<?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>
最后,查看以下链接了解详细信息。
如果有什么我能帮忙的,请随时告诉我