-
-
Save Ellisonlee/41f9ed08c6d20d7d9cdb12c73617d165 to your computer and use it in GitHub Desktop.
simple nodejs http proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require("http"); | |
var url = require("url"); | |
var proxy = http.createServer(function(request, response) { | |
request.headers.Connection = "close"; | |
delete request.headers["proxy-connection"]; | |
delete request.headers["upgrade-insecure-requests"]; | |
delete request.headers['accept-encoding']; | |
var ori_url = url.parse(request.url); | |
var options = { | |
method: request.method, | |
path: ori_url.path, | |
hostname: ori_url.hostname, | |
port: ori_url.port | 80, | |
headers: request.headers | |
}; | |
var req = http.request(options, function(res) { | |
console.log(request.url); | |
res.pipe(response); | |
}); | |
switch (request.method) { | |
case "POST": | |
request.on('data', function (data) { | |
req.write(data); | |
}); | |
req.on('end', function () { | |
req.end(); | |
}); | |
break; | |
case "GET": | |
default: | |
req.end(); | |
break; | |
} | |
}).listen(9000); |
POST
方法也没试过,只有简单GET的request试过没问题。
这个例子很简陋,参考需谨慎。。。
port: ori_url.port | 80
=> port: ori_url.port || 80
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
我又改了一下,
request.headers.Connection
的c
好像不应该大写...