이것은 간단한 프록시 서버 트래픽을 스니핑하고 콘솔에 쓰기 :
var http = require('http'),
httpProxy = require('http-proxy');
//
// Create a proxy server with custom application logic
//
var proxy = httpProxy.createProxyServer({});
// assign events
proxy.on('proxyRes', function (proxyRes, req, res) {
// collect response data
var proxyResData='';
proxyRes.on('data', function (chunk) {
proxyResData +=chunk;
});
proxyRes.on('end',function() {
var snifferData =
{
request:{
data:req.body,
headers:req.headers,
url:req.url,
method:req.method},
response:{
data:proxyResData,
headers:proxyRes.headers,
statusCode:proxyRes.statusCode}
};
console.log(snifferData);
});
// console.log('RAW Response from the target', JSON.stringify(proxyRes.headers, true, 2));
});
proxy.on('proxyReq', function(proxyReq, req, res, options) {
// collect request data
req.body='';
req.on('data', function (chunk) {
req.body +=chunk;
});
req.on('end', function() {
});
});
proxy.on('error',
function(err)
{
console.error(err);
});
// run the proxy server
var server = http.createServer(function(req, res) {
// every time a request comes proxy it:
proxy.web(req, res, {
target: 'http://localhost:4444'
});
});
console.log("listening on port 5556")
server.listen(5556);
이 중대하다, 감사합니다! 그러나 실제로 헤더를 클라이언트에 다시 보내기 전에이를 다시 작성해야합니다. 귀하의 예는 데이터를 듣기/녹음 만하고 변경하지는 않습니다. 클라이언트에게 다시 전송되기 전에 실제로 그것을 변경하는 방법에 대한 아이디어가 있습니까? – Tauren
@Tauren - 수정 된 데이터를 전송해야하는 경우 다음 세 가지가 필요합니다. 1. 들어오는 데이터를 읽습니다. 2. 수정합니다. 3. 앞으로 보냅니다. 'node-http-proxy'의 요점은 프록시 프로세스를 캡슐화하는 것입니다. 그리고 여러분의 경우에는별로 도움이되지 않을 것입니다. 따라서 노드의 내장 HTTP 서버와 mikeal의 우수한 [요청 라이브러리] (https://github.com/mikeal/request)를 HTTP 클라이언트로 사용하여 프록시를 패치하는 것이 좋습니다. 피드백에 대해 – zzen
에게 감사드립니다.그 결론은 내가 왔던 결론과 같지만, 그것을 달성하고 노드 -http-proxy를 사용하는 방법이있을 수 있기를 바랬다. 그것은 내 필요의 99 %를 해결하고 있지만,이 문제로 인해 문제가 발생합니다. – Tauren