我需要创建应用程序来获取特定端口的请求并将其代理到不同端口上的新服务器
例如,以下端口 3000 将成为端口 9000 的代理,并且您实际上在 9000 上运行应用程序(在后台),因为客户端中的用户单击 3000
我尝试类似
var proxy = httpProxy.createProxyServer({});
http.createServer(function (req, res) {
var hostname = req.headers.host.split(":")[0];
var pathname = url.parse(req.url).pathname;
proxy.web(req, res, {
target: 'http://' + hostname + ':' + 9000
});
var proxyServer = http.createServer(function (req, res) {
res.end("Request received on " + 9000);
});
proxyServer.listen(9000);
}).listen(3000, function () {
});
- 是正确的方法吗?
- 我该如何测试它?我问,因为如果我在端口 3000 中运行节点应用程序,我不能将第一个 URL 放在客户端http://localhost:3000/a/b/c中,因为该端口已经被占用。有解决方法吗?