Created
February 29, 2016 08:56
-
-
Save garylgh/269ada956f52776d0697 to your computer and use it in GitHub Desktop.
A http proxy with nodejs
This file contains hidden or 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 proxy = http.createServer(function(request, response) { | |
var options = { | |
host: 'proxy.xxxx.com', // 这里是代理服务器 | |
port: 8080, // 这里是代理服务器端口 | |
path: request.url, | |
method: request.method, | |
headers: { | |
// 如果代理服务器需要认证 | |
'Proxy-Authentication': 'Base ' + new Buffer('user:password').toString('base64') // 替换为代理服务器用户名和密码 | |
} | |
}; | |
var req = http.request(options, function(req, res) { | |
res.pipe(response); // 这个pipe很喜欢 | |
console.log(req.url); | |
}).end(); | |
}).listen(8080); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment