http-proxy-middleware 没有代理到其他服务器的索引页面?
http-proxy-middleware not proxying to index page of other server?
我在 Docker 上有两台服务器 运行。一个是 localhost:3000
的 React 前端,而后端在 localhost:9000
运行。当我去localhost:3000/api的时候,我想去后台的index页面,也就是localhost:9000
.
在通过 create-react-app 创建的 myApp 文件夹中创建了一个 setupProxy.js 文件:
const proxy = require('http-proxy-middleware');
module.exports = function(app) {
app.use(proxy('/api', { target: 'http://backend:9000' }));
};
当我转到 localhost:3000/api
时,我被发送到 localhost:9000/api
而不是 localhost:9000
。
http-proxy-middleware
有一个pathRewrite
选项,见documentation.
在您的具体情况下:
const proxy = require('http-proxy-middleware');
module.exports = function(app) {
app.use(proxy('/api', {
target: 'http://backend:9000',
pathRewrite: {'^/api' : ''}
}));
};
这通常应该将 localhost:3000/api/endpoint
重写为 localhost:9000/endpoint
。
请注意,还有一个 router
选项可用于定制更多行为。
我在 Docker 上有两台服务器 运行。一个是 localhost:3000
的 React 前端,而后端在 localhost:9000
运行。当我去localhost:3000/api的时候,我想去后台的index页面,也就是localhost:9000
.
在通过 create-react-app 创建的 myApp 文件夹中创建了一个 setupProxy.js 文件:
const proxy = require('http-proxy-middleware');
module.exports = function(app) {
app.use(proxy('/api', { target: 'http://backend:9000' }));
};
当我转到 localhost:3000/api
时,我被发送到 localhost:9000/api
而不是 localhost:9000
。
http-proxy-middleware
有一个pathRewrite
选项,见documentation.
在您的具体情况下:
const proxy = require('http-proxy-middleware');
module.exports = function(app) {
app.use(proxy('/api', {
target: 'http://backend:9000',
pathRewrite: {'^/api' : ''}
}));
};
这通常应该将 localhost:3000/api/endpoint
重写为 localhost:9000/endpoint
。
请注意,还有一个 router
选项可用于定制更多行为。