设置具有多个目标的代理服务器?

Set up proxy server with multiple targets?

我有 2 台快递服务器:

  1. api1
  2. api2

两者都在本地使用:

http://localhost:3000/news

http://localhost:3001/stock

我的目标:

http://localhost:8008

上的代理服务器访问两个快速服务器端点

我的问题:

我只能命中第一个 api1 的端点

任何帮助将不胜感激:)

const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');

const api1 = createProxyMiddleware({
  target: 'http://localhost:3000'
});


const api2 = createProxyMiddleware({
    target: 'http://localhost:3001'
  });

const app = express();

app.use(api1);
app.use(api2);

app.listen(8008);

**edit1:

我试过了,它适用于 api1 端点但不适用于 api2 端点。

const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');

const apiProxy = createProxyMiddleware('/', {
  target: 'http://localhost:3000'
});
const apiProxytwo = createProxyMiddleware('/', {
  target: 'http://localhost:3001'
});

const app = express();

app.use(apiProxy,apiProxytwo);
app.listen(8008);

仍在寻找解决方案!!@!请帮忙!

编辑 2:工作解决方案

const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();


app.use('/news/*', createProxyMiddleware({ target: 'http://localhost:3000', changeOrigin: true ,}));
app.use('/stock/', createProxyMiddleware({ target: 'http://localhost:3001', changeOrigin: true, }));

app.listen(8008);

编辑 3(*奖励):在容器设置中应用解决方案 - 必须将本地主机更改为容器名称(newsstock) 以便代理正确处理容器。

const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();


app.use('/news/*', createProxyMiddleware({ target: 'http://news:3000', changeOrigin: true ,}));
app.use('/stock/', createProxyMiddleware({ target: 'http://stock:3001', changeOrigin: true, }));

app.listen(8008);

proxy.js:

const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();


app.use('/news/*', createProxyMiddleware({ target: 'http://localhost:3000', changeOrigin: true }));
app.use('/stock', createProxyMiddleware({ target: 'http://localhost:3001', changeOrigin: true }));

app.listen(8008);

newsServer.js

const express = require('express');
const app = express();

app.get('/news/headlines', (req, res) => {
    res.send('hello from headlines')
});

app.get('/news/other', (req, res) => {
    res.send('hello from other')
})


app.listen(3000);

查询http://localhost:8008/news/headlines=>'hello from headlines'

stockServer.js

const express = require('express');
const app = express();

app.get('/stock', function(req, res) {
    res.send('hello from api2');
  });


app.listen(3001);

查询http://localhost:8008/stock=>'hello from api2'

所有建议的解决方案都不适用于我的情况。然而,在仔细检查 http-proxy-middleware options 文档后,根据路径、主机和任何其他自定义案例更改目标的唯一方法是使用路由器选项

因此:

const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();


app.use('/news/*', createProxyMiddleware({ target: 'http://localhost:3000', changeOrigin: true }));
app.use('/stock', createProxyMiddleware({ target: 'http://localhost:3001', changeOrigin: true }));

app.listen(8008);

将是:

app.use("**", createProxyMiddleware({
  target: "http://localhost",
  router:{
    "/news":"http://localhost:3000",
    "/stock":"http://localhost:3001"
  }
}));

参见:http-proxy-middleware options