Skip to content

Instantly share code, notes, and snippets.

@garylgh
Created February 29, 2016 08:56
Show Gist options
  • Save garylgh/269ada956f52776d0697 to your computer and use it in GitHub Desktop.
Save garylgh/269ada956f52776d0697 to your computer and use it in GitHub Desktop.
A http proxy with nodejs
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