我想在我的浏览器中使用node.js构建代理。 我在谷歌搜索并看到example,但有些Apis已被弃用。
所以我改变了以下代码:
var http = require('http');
http.createServer(function(request, response) {
console.log("req for:"+ request.headers['host'] + "\nmethod: "+ request.method);
var options = {
host : request.headers['host'],
method : request.method,
headers : request.headers
}
var callback = function(res){
// get the proxyclient response and write to the original response
res.addListener('data', function(chunk){
console.log("writing response");
response.write(chunk, 'binary');
});
res.addListener('end', function(){
console.log("end response");
response.end();
});
};
// send the original request
var proxy = http.request(options, callback);
// intercept the request and write to proxyclient
request.addListener('data', function(chunk){
console.log("new request");
proxy.write(chunk, 'binary');
});
request.addListener('end', function(){
console.log("end request");
proxy.end();
});
}).listen(3333);
之后,我将浏览器代理设置更改为使用localhost:3333。 我的问题是事件"数据"没有被解雇。
这应该像我想的那样工作?
答案 0 :(得分:0)
也许你写这段代码;
response.writeHead(res.statusCode, res.headers); after res.addListener('end', function(){
console.log("end response");
response.end();
});