我有一个React应用程序,它是由React-app和Redux以及Node.js(特别是Node.js)后端创建的。在前端package.json文件中,我设置了“proxy:http://localhost:5000”将所有API调用从react应用重定向到后端服务器。同时,后端服务器在本地主机上同时运行和侦听:5000端口。
应用程序结构如下所示:App Structure
前端
客户端文件夹(前端)中的package.json文件如下设置代理
{
...,
"proxy": "http://localhost:5000"
}
在App.js(前端入口点)中,我用react-router-dom创建了一堆URL路由:
return (
<ThemeProvider theme={theme}>
<Provider store={store}>
<Fragment>
<Router>
<div
className='App'
style={{ fontFamily: 'Montserrat', fontWeight: '500' }}>
<header>
<Navbar />{' '}
</header>
<div>
<Routes>
<Route exact path='/' element={<Home />} />
<Route exact path='/home' element={<Home />} />
<Route exact path='/dashboard' element={<Dashboard />} />
<Route exact path='/about' element={<About />} />
<Route exact path='/register' element={<Register />} />
<Route exact path='/login' element={<Login />} />
<Route exact path='/forgot' element={<ForgotPassword />} />
<Route path='/reset' element={<ResetPassword />} />
<Route path='/avatar' element={<ChangeAvatar />} />
<Route
exact
path='/systemtables'
element={<SystemTables />}
/>
<Route exact path='/profiles' element={<ProfileMain />} />
<Route exact path='/companies' element={<CompanyMain />} />
...
</Routes>
</div>
</div>
</Router>
</Fragment>
</Provider>
</ThemeProvider>
);
后台
应用根目录(AAQCS文件夹)中的package.json文件如下所示:
{
...,
"scripts": {
"start": "node index.js",
"server": "nodemon index.js --ignore temp/",
"client": "npm start --prefix client",
"clientinstall": "npm install -prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\""
},
"devDependencies": {
"concurrently": "^6.0.2",
"nodemon": "^2.0.19"
}
}
在“web-server.js”(后端入口点)中,我有以下代码:
const app = express();
app.use(cors());
httpServer = http.createServer(app);
//Set server side static uploads folder receiving front-end upload request.
app.use('/public', express.static('public'));
// Combines logging info from request and response
app.use(morgan('combined'));
//Init Middleware to receive request body
app.use(express.json({ extended: false, limit: '50000mb' }));
app.use(favicon(__dirname + '/images/favicon.ico'));
//Serve any static files built by React
app.use(express.static(path.join(__dirname, 'client/build')));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
//Define Routes
app.use('/api/users', require('./routes/users'));
app.use('/api/auth', require('./routes/auth'));
app.use('/api/asset', require('./routes/asset'));
app.use('/api/companies', require('./routes/companies'));
...
httpServer
.listen(webServerConfig.port)
.on('listening', () => {
console.log(
`Web server listening on localhost:${webServerConfig.port}`
);
resolve();
})
.on('error', (err) => {
reject(err);
});
后端服务器监听本地主机:5000。
目前我在web.config文件中有这些规则:
<rules>
<clear />
<rule name="RedirectToBackend" stopProcessing="true">
<match url="^api/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="http://localhost:5000/api/{R:1}" />
</rule>
<rule name="React Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<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="Redirect" url="{R:0}" />
</rule>
</rules>
当我在开发环境中时,一切都正常,所有的路由都正常工作。
在我把构建放在IIS中后,我可以启动登录页面,但当我点击登录按钮时,我得到错误:“POST http://localhost:3000/api/auth
500(URL重写模块错误。)"
我的问题是:
1.我的web.config文件中的路由规则有什么问题吗?
1.基本上,我想实现的是所有格式为“/API/xxxx”的API调用都应该重定向到后端服务器正在监听的localhost:5000。所有其他没有“/API/xxxx”格式的请求都应该遵循react应用程序中设置的路由。如何制定规则来实现这一点。
1条答案
按热度按时间nx7onnlm1#
您需要安装ARR并启用代理功能,以作为反向代理与IIS一起工作,它可以将所有格式为“/API/xxxx”的API调用转发到后端NodeJS服务器。
确保URL重写操作类型设置为“重写”而不是“重定向”,因为您希望代理请求而不是执行重定向。
此外,所有其他没有“/API/xxxx”格式的请求都应该遵循React应用程序中设置的路由。您需要创建以下规则,将所有非API请求重写到根URL(“/”),React应用程序的路由将处理它们。