检查新端口上的应用 运行
Check app running on new port
我需要创建应用程序获取特定端口的请求并将其代理到不同端口上的新服务器
例如以下端口 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 中,因为这个端口已经被占用。有解决方法吗?
很少examples介绍代理服务器的各种用法。这是一个基本代理服务器的简单示例:
var http = require("http");
var httpProxy = require('http-proxy');
/** PROXY SERVER **/
var proxy = httpProxy.createServer({
target:'http://localhost:'+3000,
changeOrigin: true
})
// add custom header by the proxy server
proxy.on('proxyReq', function(proxyReq, req, res, options) {
proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
});
proxy.listen(8080);
/** TARGET HTTP SERVER **/
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
//check if the request came from proxy server
if(req.headers['x-special-proxy-header'])
console.log('Request received from proxy server.')
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(3000);
测试代理服务器是否工作或请求是否来自代理服务器:
我添加了一个 proxyReq
侦听器,它添加了一个自定义 header。您可以从这个 header 判断请求是否来自代理服务器。
因此,如果您访问 http://localhost:8080/a/b/c
,您会看到 req.headers
有这样一个 header:
'X-Special-Proxy-Header': 'foobar'
这个header只有在client向8080端口发起请求时才会设置
但对于 http://localhost:3000/a/b/c
,您不会看到这样的 header,因为客户端正在绕过代理服务器并且从未设置 header。
我需要创建应用程序获取特定端口的请求并将其代理到不同端口上的新服务器
例如以下端口 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 中,因为这个端口已经被占用。有解决方法吗?
很少examples介绍代理服务器的各种用法。这是一个基本代理服务器的简单示例:
var http = require("http");
var httpProxy = require('http-proxy');
/** PROXY SERVER **/
var proxy = httpProxy.createServer({
target:'http://localhost:'+3000,
changeOrigin: true
})
// add custom header by the proxy server
proxy.on('proxyReq', function(proxyReq, req, res, options) {
proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
});
proxy.listen(8080);
/** TARGET HTTP SERVER **/
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
//check if the request came from proxy server
if(req.headers['x-special-proxy-header'])
console.log('Request received from proxy server.')
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(3000);
测试代理服务器是否工作或请求是否来自代理服务器:
我添加了一个 proxyReq
侦听器,它添加了一个自定义 header。您可以从这个 header 判断请求是否来自代理服务器。
因此,如果您访问 http://localhost:8080/a/b/c
,您会看到 req.headers
有这样一个 header:
'X-Special-Proxy-Header': 'foobar'
这个header只有在client向8080端口发起请求时才会设置
但对于 http://localhost:3000/a/b/c
,您不会看到这样的 header,因为客户端正在绕过代理服务器并且从未设置 header。