あなたの質問を正しく読んだ場合、ノード プロキシ ルーティング テーブルを使用して、リクエスト オブジェクトに基づいてさまざまなホストへのリクエストをルーティングする方法を知っている必要があります。これは実際には node-http-proxy docs で対処されています: Proxy requests using a ProxyTable
それを貼り付けて、次のように JS オブジェクトでパスを構成するだけです。
var options = {
router: {
'foo.com/baz': '127.0.0.1:8001',
'foo.com/buz': '127.0.0.1:8002',
'bar.com/buz': '127.0.0.1:8003'
}
};
次に、プロキシ サーバーを作成するときにこのオブジェクトを渡します。
var proxyServer = httpProxy.createServer(options);
proxyServer.listen(80);
これをコードに指定すると、次のようになります。
var http = require('http'),
httpProxy = require('http-proxy'),
endserver = 'server.local',
endport = 8443;
var options = {
router: {
endserver + '/foo': '127.0.0.1:8081',
endserver + '/bar': '127.0.0.1:8082',
endserver + '/baz': '127.0.0.1:8083'
}
};
var proxyServer = httpProxy.createServer(options);
proxyServer.listen(endport);
http.createServer(function (req, res) {
// your code for modifying response is here...
proxy.proxyRequest(req, res);
}).listen(8001);
ルーターオプションを使用する場合、ターゲットを自分で設定する必要はありません。httpsの区別も扱っているようですが、よくわかりません。この情報はproxy-table.js sourceから取得しました。特に:
ProxyTable.prototype.setRoutes = function (router) {
if (!router) {
throw new Error('Cannot update ProxyTable routes without router.');
}
var self = this;
this.router = router;
if (this.hostnameOnly === false) {
this.routes = [];
Object.keys(router).forEach(function (path) {
if (!/http[s]?/.test(router[path])) {
router[path] = (self.target.https ? 'https://' : 'http://')
+ router[path];
}
var target = url.parse(router[path]),
defaultPort = self.target.https ? 443 : 80;
//
// Setup a robust lookup table for the route:
//
// {
// source: {
// regexp: /^foo.com/i,
// sref: 'foo.com',
// url: {
// protocol: 'http:',
// slashes: true,
// host: 'foo.com',
// hostname: 'foo.com',
// href: 'http://foo.com/',
// pathname: '/',
// path: '/'
// }
// },
// {
// target: {
// sref: '127.0.0.1:8000/',
// url: {
// protocol: 'http:',
// slashes: true,
// host: '127.0.0.1:8000',
// hostname: '127.0.0.1',
// href: 'http://127.0.0.1:8000/',
// pathname: '/',
// path: '/'
// }
// },
//
self.routes.push({
source: {
regexp: new RegExp('^' + path, 'i'),
sref: path,
url: url.parse('http://' + path)
},
target: {
sref: target.hostname + ':' + (target.port || defaultPort) + target.path,
url: target
}
});
});
}
};