如何使用 http-proxy-middleware 和 express 通过请求主机名设置代理?
how to set proxy by request hostname with http-proxy-middleware and express?
我想用 http-proxy-middleware 和 express 配置代理。
规则是主机名的映射,例如:
http://123.com >> http://localhost:3000/123
http://456.com >> http://localhost:3000/abc
我试过这样:
import express from 'express';
import http from 'http';
import proxy from 'http-proxy-middleware';
const app = express();
app.use( async function (req, res) {
let direction = 'http://localhost:3000';
console.log('hostname: ', req.hostname);
console.log('originalUrl: ', req.originalUrl);
if (req.hostname == '123.com') {
direction = `http://localhost:3000/123${req.originalUrl}`;
}
if (req.hostname == '456.com') {
direction = `http://localhost:3000/abc${req.originalUrl}`;
}
return await proxy({ target: direction, changeOrigin: false })
});
const server = http.createServer(app);
app.set('port', '127.0.0.1');
server.listen(9999, '0.0.0.0');
但是没用。
您需要考虑以下几点:
- http-proxy-middleware 模块没有 return 承诺,而是
它 return 是一个快速中间件。
- 您可以使用自定义过滤器来决定是否代理请求。
- 您需要添加 pathRewrite 选项才能根据当前主机名重写 url。
- 另一种选择是使用路由器选项。见 relevant documentation.
我写了一个快速的 express 应用程序来测试它(请注意,我用指向 127.0.0.1 的 localwebapp
和 localwebapp2
覆盖了我的主机文件)并且它似乎工作正常:
const express = require('express')
const proxy = require('http-proxy-middleware')
const app = express();
const filter = (pathname, req) => {
if (req.hostname == 'localwebapp' || req.hostname == 'localwebapp2') {
return true;
}
return false;
};
app.get('/123*', (req, res) => {
res.send(`matched 123* route: ${req.path}`);
})
app.get('/abc*', (req, res) => {
res.send(`matched abc* route: ${req.path}`);
})
app.get('/test', (req, res) => {
res.send("matched non proxied route '/test'");
})
const apiProxy = proxy(filter, {
target: 'http://localhost:3000',
logLevel: 'debug',
changeOrigin: true,
pathRewrite: function (path, req) {
if (req.hostname == 'localwebapp') {
return `/123${req.originalUrl}`;
}
if (req.hostname == 'localwebapp2') {
return `/abc${req.originalUrl}`;
}
return path;
}
})
app.use(apiProxy)
app.listen(3000);
我想用 http-proxy-middleware 和 express 配置代理。 规则是主机名的映射,例如:
http://123.com >> http://localhost:3000/123
http://456.com >> http://localhost:3000/abc
我试过这样:
import express from 'express';
import http from 'http';
import proxy from 'http-proxy-middleware';
const app = express();
app.use( async function (req, res) {
let direction = 'http://localhost:3000';
console.log('hostname: ', req.hostname);
console.log('originalUrl: ', req.originalUrl);
if (req.hostname == '123.com') {
direction = `http://localhost:3000/123${req.originalUrl}`;
}
if (req.hostname == '456.com') {
direction = `http://localhost:3000/abc${req.originalUrl}`;
}
return await proxy({ target: direction, changeOrigin: false })
});
const server = http.createServer(app);
app.set('port', '127.0.0.1');
server.listen(9999, '0.0.0.0');
但是没用。
您需要考虑以下几点:
- http-proxy-middleware 模块没有 return 承诺,而是 它 return 是一个快速中间件。
- 您可以使用自定义过滤器来决定是否代理请求。
- 您需要添加 pathRewrite 选项才能根据当前主机名重写 url。
- 另一种选择是使用路由器选项。见 relevant documentation.
我写了一个快速的 express 应用程序来测试它(请注意,我用指向 127.0.0.1 的 localwebapp
和 localwebapp2
覆盖了我的主机文件)并且它似乎工作正常:
const express = require('express')
const proxy = require('http-proxy-middleware')
const app = express();
const filter = (pathname, req) => {
if (req.hostname == 'localwebapp' || req.hostname == 'localwebapp2') {
return true;
}
return false;
};
app.get('/123*', (req, res) => {
res.send(`matched 123* route: ${req.path}`);
})
app.get('/abc*', (req, res) => {
res.send(`matched abc* route: ${req.path}`);
})
app.get('/test', (req, res) => {
res.send("matched non proxied route '/test'");
})
const apiProxy = proxy(filter, {
target: 'http://localhost:3000',
logLevel: 'debug',
changeOrigin: true,
pathRewrite: function (path, req) {
if (req.hostname == 'localwebapp') {
return `/123${req.originalUrl}`;
}
if (req.hostname == 'localwebapp2') {
return `/abc${req.originalUrl}`;
}
return path;
}
})
app.use(apiProxy)
app.listen(3000);