我使用了代理 - https://github.com/pkrumins/nodejs-proxy。优秀的工作,但我需要修改响应数据到客户端,所以如果我更改代码(代码片段):
proxy_request.addListener('response', function(proxy_response) {
if(legacy_http && proxy_response.headers['transfer-encoding'] != undefined){
console.log("legacy HTTP: "+request.httpVersion);
//filter headers
var headers = proxy_response.headers;
delete proxy_response.headers['transfer-encoding'];
var buffer = "";
//buffer answer
proxy_response.addListener('data', function(chunk) {
buffer += chunk;
});
proxy_response.addListener('end', function() {
headers['Content-length'] = buffer.length;//cancel transfer encoding "chunked"
response.writeHead(proxy_response.statusCode, headers);
response.write(buffer, 'binary');
response.end();
});
} else {
proxy_response.process = function(chunk){
proxy_response.buf = "";
chunk = this.buf + chunk;
chunk = chunk.replace('</body>', "my html here</body>");
response.write(chunk, 'binary');
};
//easy data forward
proxy_response.addListener('data', function(chunk) {
response.write(chunk, 'binary');
});
proxy_response.addListener('end', function() {
response.end();
});
//send headers as received
//response.writeHead(proxy_response.statusCode, proxy_response.headers);
}
});
但是这段代码不起作用。有人可以帮我修改数据吗?感谢。